Visual Basic.NET 2008 > Printing with Visual Basic

<< Previous Page Next Chapter - Basic Concepts of Relational Databases >>

Printing Bitmaps - Visual Basic 2008

If you have a color printer, you probably want to print images, too. Actually, most black-and-white printers print images in grayscale too, so you can experiment with the material of this chapter even if you have only a black-and-white laser printer. As you have probably guessed, you call the DrawImage method to send the bitmap to the printer. As a reminder, the simplest form of the DrawImage method of the Graphics object accepts two arguments, the bitmap to be drawn (an Image object) and a rectangle in which the image will be drawn:

Graphics.DrawImage(image, rectangle)

The method will stretch the bitmap specified by the image argument to fill the rectangle specified by the rectangle argument. It's imperative that you carefully calculate the dimensions of the rectangle, so that they will retain their original aspect ratio. If not, the image will be distorted in the process. Most applications will let the user specify a zoom factor, and then apply it to both dimensions. If the image fits on the page in actual size, you can make the rectangle equal to the dimensions of the image and not worry about distortions.

Because the reduced image will, most likely, be smaller than the dimensions of the paper on which it will be printed, you must also center the image on the paper. To do so, you can subtract the image's width from the paper's width and split the difference on the two sides of the image (you will do the same for the vertical margins). These operations will be demonstrated with the code of the PrintBitmap application, whose main form is shown in Figure 16.10. The application allows you to load an image and zoom in or out. The Zoom > Auto command resizes the image to fit the current size of the form as best as possible, while the Zoom > Normal command displays the image in actual size, regardless of whether it fits on the form or not. If not, the appropriate scroll bars will be attached automatically, because the form's AutoSize property is set to True.

The PrintBitmap application resizes and rotates bitmaps to best fit the width of the page and prints them

Figure 16.10 - The PrintBitmap application resizes and rotates bitmaps to best fit the width of the page and prints them.

If you specify a rectangle the same size as the image, the image will be printed at its actual size. A common image resolution is 72 dots per inch. If the bitmap is 1,024 pixels wide, it will take approximately 14 inches across the page — this means that part of the image won't be printed.

If the bitmap is too large for a letter-size page, you must reduce its size. The following statements, which must appear in the PrintDocument event, print the image centered on the page.

If the image doesn't fit on the page, its top-left corner is printed at the origin, and the rightmost and bottommost parts of the image will be cropped. Notice also that the image isn't printed in actual size; instead, it's printed at the current magnification. Listing 16.10 provides the code of the PrintPage event handler.

Listing 16.10: Scaling and Printing a Bitmap

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
        Handles PrintDocument1.PrintPage
    Dim R As Rectangle
    Dim PWidth, PHeight As Integer
    Dim PictLeft, PictTop As Integer
    PWidth = PictureBox1.Width
    PHeight = PictureBox1.Height
    With PrintDocument1.DefaultPageSettings.PaperSize
        If PWidth < .Width Then
            PictLeft = Convert.ToInt32((.Width - PWidth) / 2)
            PictLeft = 0
        End If
        If PHeight < .Height Then
            PictTop = Convert.ToInt32((.Height - PHeight) / 2)
            PictTop = 0
        End If
    End With
    R = New Rectangle(PictLeft, PictTop, Convert.ToInt32(PWidth), _
    e.Graphics.DrawImage(PictureBox1.Image, R)
End Sub

The PWidth and PHeight variables hold the dimensions of the scaled image, whereas PictLeft and PictTop are the coordinates of the image's top-left corner on the page. To initiate the printing process, you must call the PrintDocument object's Print method, or you can display the Print Preview dialog box, which is what the following code does:

Private Sub bttnPrint Click(...) Handles bttnPrint.Click
    PrintPreviewDialog1.Document = PrintDocument1
End Sub

The PrintBitmap application's main form

Figure 16.11 The PrintBitmap application's main form

The PrintBitmap application allows the user to resize and rotate the image before printing it. These rotation commands can be found in the main form's Process menu; the Zoom menu has four options: Auto, Normal, Zoom In, and Zoom Out (Figure 16.11). The last two commands zoom in and out, respectively, by 25 percent at a time. These commands change the size of the PictureBox control that holds the image, and the PrintPage event handler uses the dimensions of this control to determine the dimensions of the printed image. The Normal command resets the image to its actual size, and the Auto command resizes the image proportionally so that its height is 400 pixels.

<< Previous Page Next Chapter - Basic Concepts of Relational Databases >>


Table of Contents

  1. Printing With Visual Basic
  2. The PrintDocument Control
  3. The PrintDialog Control
  4. The PageSetupDialog Control
  5. The PrintPreviewDialog Control
  6. Printer and Page Properties
  7. Retrieving the Printer Names & Page Geometry
  8. The SimplePrintout Example
  9. The PageSettings Example
  10. Printing Tabular Data
  11. Printing Plain Text
  12. Printing Bitmaps

 Copyright 2011 All Rights Reserved
  Home | Useful links | Contact us