How do I print the contents of a picture box to the printer. It seems that the PrintForm method only prints a form to a printer and not a picture box. PK Odendaal
and pko@942.co.za
and pko@942.co.za
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
' Printing using the VB printer object
'
Private Sub CmdPrint_Click()
Dim oldcursor&
oldcursor = Screen.MousePointer
Screen.MousePointer = 11
Printer.Print " " ' Convince VB that something should be printed
PrintBitmap Printer.hdc
Printer.NewPage
Printer.EndDoc
Screen.MousePointer = oldcursor
End Sub
' Prints the bitmap in the picture1 control to the
' printer context specified.
'
Private Sub PrintBitmap(hdc&)
Dim bi As BITMAPINFO
Dim dctemp&, dctemp2&
Dim msg$
Dim bufsize&
Dim bm As BITMAP
Dim ghnd&
Dim gptr&
Dim xpix&, ypix&
Dim doscale&
Dim uy&, ux&
Dim di&
' Create a temporary memory DC and select into it
' the background picture of the picture1 control.
dctemp& = CreateCompatibleDC(Picture1.hdc)
' Get the size of the picture bitmap
di = GetObjectAPI(Picture1.Picture, Len(bm), bm)
' Can this printer handle the DIB?
If (GetDeviceCaps(hdc, RASTERCAPS)) And RC_DIBTODEV = 0 Then
msg$ = "This device does not support DIB's" + vbCrLf + "See source code for further info"
MsgBox msg$, 0, "No DIB support"
End If
' Fill the BITMAPINFO for the desired DIB
bi.bmiHeader.biSize = Len(bi.bmiHeader)
bi.bmiHeader.biWidth = bm.bmWidth
bi.bmiHeader.biHeight = bm.bmHeight
bi.bmiHeader.biPlanes = 1
' Set to 24 here to create a 24 bit DIB
' Set to 8 here to create an 8 bit DIB
bi.bmiHeader.biBitCount = 4
bi.bmiHeader.biCompression = BI_RGB
' Now calculate the data buffer size needed
bufsize& = bi.bmiHeader.biWidth
' Figure out the number of bytes based on the
' number of pixels in each byte. In this case we
' really don't need all this code because this example
' always uses a 16 color DIB, but the code is shown
' here for your future reference
Select Case bi.bmiHeader.biBitCount
Case 1
bufsize& = Int((bufsize& + 7) / 8)
Case 4
bufsize& = Int((bufsize& + 1) / 2)
Case 24
bufsize& = bufsize& * 3
End Select
' And make sure it aligns on a long boundary
bufsize& = (Int((bufsize& + 3) / 4)) * 4
' And multiply by the # of scan lines
bufsize& = bufsize& * bi.bmiHeader.biHeight
' Now allocate a buffer to hold the data
' We use the global memory pool because this buffer
' could easily be above 64k bytes.
ghnd = GlobalAlloc(GMEM_MOVEABLE, bufsize&)
gptr& = GlobalLock&(ghnd)
di = GetDIBits(dctemp, Picture1.Picture, 0, bm.bmHeight, ByVal gptr&, bi, DIB_RGB_COLORS)
di = SetDIBitsToDevice(hdc, 0, 0, bm.bmWidth, bm.bmHeight, 0, 0, 0, bm.bmHeight, ByVal gptr&, bi, DIB_RGB_COLORS)
' Now see if we can also print a scaled version
xpix = GetDeviceCaps(hdc, HORZRES)
' We subtract off the size of the bitmap already
' printed, plus some extra space
ypix = GetDeviceCaps(hdc, VERTRES) - (bm.bmHeight + 50)
' Find out the largest multiplier we can use and still
' fit on the page
doscale = xpix / bm.bmWidth
If (ypix / bm.bmHeight < doscale) Then doscale = ypix / bm.bmHeight
If doscale > 1 Then
doscale = doscale
ux = bm.bmWidth * doscale
uy = bm.bmHeight * doscale
' Now how this is offset a bit so that we don't
' print over the 1:1 scaled bitmap
di = StretchDIBits(hdc, 0, bm.bmHeight + 50, ux, uy, 0, 0, bm.bmWidth, bm.bmHeight, ByVal gptr&, bi, DIB_RGB_COLORS, SRCCOPY)
End If
' Dump the global memory block
di = GlobalUnlock(ghnd)
di = GlobalFree(ghnd)
di = DeleteDC(dctemp)
End Sub