Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePrint a picture centered
KeywordsPictureBox, picture, print
CategoriesGraphics
 
Use ScaleX and ScaleY to see how big the picture will be in printer coordinates. Subtract these from the Printer's ScaleWidth and ScaleHeight properties to see where the picture needs to be drawn to center it. Then use Printer.PaintPicture to draw the picture on the printer.
 
' Print the PictureBox's picture centered and
' draw a box around it.
Private Sub cmdPrint_Click()
Dim wid As Single
Dim hgt As Single
Dim X As Single
Dim Y As Single

    ' Set the PictureBox's ScaleMode to pixels to
    ' make things interesting.
    picCanvas.ScaleMode = vbPixels

    ' Get the picture's dimensions in the printer's
    ' scale mode.
    wid = ScaleX(picCanvas.ScaleWidth, picCanvas.ScaleMode, _
        Printer.ScaleMode)
    hgt = ScaleY(picCanvas.ScaleHeight, _
        picCanvas.ScaleMode, Printer.ScaleMode)

    ' See where we need to print to center the picture.
    X = Printer.ScaleLeft + (Printer.ScaleWidth - wid) / 2
    Y = Printer.ScaleTop + (Printer.ScaleHeight - hgt) / 2

    ' Print the picture.
    Printer.PaintPicture picCanvas.Picture, X, Y

    ' Draw the box.
    Printer.Line (X, Y)-Step(wid, hgt), , B

    ' Finish printing.
    Printer.EndDoc

    MsgBox "Done"
End Sub
 
For more information on printing and graphics in Visual Basic, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated