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 PictureBox and any controls that are on top of or inside it
KeywordsPictureBox, print, keybd_event, fit to printer
CategoriesGraphics, Controls
 
This program uses the keybd_event API function to simulate pressing Alt-PrntScrn to place an image of the form in the clipboard. Itpastes the image into a hidden PictureBox. Then it determines where inside the image the target PictureBox lies and uses PaintPicture to copy that part of the image to the Printer object.
 
' Print an image of the control and any
' controls on top of it.
' Be sure the form containing the control has
' the focus before you call this routine.
Private Sub PrintPictureBoxImage(ByVal ptr As Object, ByVal _
    pic As PictureBox, Optional ByVal fit_to_printer = _
    False)
Dim pic_parent As Form
Dim parent_x As Single
Dim parent_y As Single
Dim pic_pt As POINTAPI
Dim pic_x1 As Single
Dim pic_y1 As Single
Dim xmin As Single
Dim ymin As Single
Dim wid As Single
Dim hgt As Single
Dim aspect As Single
Dim pic_wid As Single
Dim pic_hgt As Single

    ' Copy the form's image to the clipboard.
    ' Press Alt.
    keybd_event VK_MENU, 0, 0, 0
    DoEvents

    ' Press Print Scrn.
'    keybd_event VK_SNAPSHOT, 1, 0, 0
    keybd_event VK_SNAPSHOT, 0, 0, 0
    DoEvents

    ' Release Alt.
    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
    DoEvents

    ' Copy the image into the hidden PictureBox.
    picHidden.Picture = Clipboard.GetData(vbCFBitmap)

    ' Get the form's location on the screen in pixels.
    Set pic_parent = pic.Parent
    parent_x = ScaleX(pic_parent.Left, vbTwips, vbPixels)
    parent_y = ScaleY(pic_parent.Top, vbTwips, vbPixels)

    ' Find the control's location on the screen in pixels.
    pic_pt.X = 0
    pic_pt.Y = 0
    ClientToScreen pic.hWnd, pic_pt

    ' Find the location of the control inside the parent
    ' form
    ' in pixels.
    pic_x1 = pic_pt.X - parent_x
    pic_y1 = pic_pt.Y - parent_y

    ' Convert into the printer's scale mode.
    pic_x1 = ScaleX(pic_x1, vbPixels, ptr.ScaleMode)
    pic_y1 = ScaleY(pic_y1, vbPixels, ptr.ScaleMode)

    ' Get the picture's size in the printer's scale mode.
    pic_wid = ScaleX(pic.ScaleWidth, pic.ScaleMode, _
        ptr.ScaleMode)
    pic_hgt = ScaleY(pic.ScaleHeight, pic.ScaleMode, _
        ptr.ScaleMode)

    ' Print the image.
    If Not fit_to_printer Then
        ' Center the image.
        wid = pic_wid
        hgt = pic_hgt
        xmin = (ptr.ScaleWidth - wid) / 2
        ymin = (ptr.ScaleHeight - hgt) / 2
    Else
        ' Make the image as large as possible
        ' without distortion.
        aspect = pic_hgt / pic_wid
        wid = ptr.ScaleWidth
        hgt = ptr.ScaleHeight
        If hgt / wid > aspect Then
            hgt = aspect * wid
            xmin = ptr.ScaleLeft
            ymin = (ptr.ScaleHeight - hgt) / 2
        Else
            wid = hgt / aspect
            xmin = (ptr.ScaleWidth - wid) / 2
            ymin = ptr.ScaleTop
        End If
    End If

    ' Print.
    ptr.PaintPicture picHidden.Picture, _
        xmin, ymin, wid, hgt, _
        pic_x1, pic_y1, pic_wid, pic_hgt
End Sub
 
For more information on graphics in Visual Basic, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated