Hi Jeffrey,
Thanks for your follow-up!
Attached is my code to print form. I've remarked the line to save image
into file and get it again. I run this code on my side, the printout is
blur, or so-called out-of-focus. If I do the save and load to get the
image, it works fine. I'm using WinXP Professional, HP 2100TN.
Moreover, I found that this way can print current form only (or say it
prints the screen actually). If I want to print any form (or say I don't
want to show the form on screen), what should I do?
Thanks again for your kindly help!
Regards,
James Wong
' >>> start of code >>>
' local variables
Private Shared objFormImg As Image ' form image
' >>>
<DllImportAttribute("gdi32.dll")> _
Public Shared Function BitBlt(ByVal hdcDest As IntPtr, ByVal nXDest As
Integer, ByVal nYDest As Int32, ByVal nWidth As Int32, ByVal nHeight As
Int32, ByVal hdcSrc As IntPtr, ByVal nXSrc As Int32, ByVal nYSrc As Int32,
ByVal dwRop As Int32) As Boolean
End Function
' >>>
Friend Shared Sub Print(ByRef frmHostForm As Form, ByVal
blnShowPrnDialog As Boolean)
' Desc.: print form
' Input: frmHostForm - (by reference) host form
containing control(s)
' blnShowPrnDialog - flag for whether show print
dialog before print
Dim objPrintDoc As New PrintDocument ' print document
Dim objPrintDialog As New PrintDialog ' print dialog
Try
'Call clsForm.gCapture(Me).Save("e:\temp\Captured.bmp",
System.Drawing.Imaging.ImageFormat.Bmp)
'objFormImg = Image.FromFile("e:\temp\Captured.bmp")
objFormImg = gCapture(frmHostForm)
If Not IsNothing(objFormImg) Then
If blnShowPrnDialog Then
With objPrintDialog
.Document = objPrintDoc
Call .ShowDialog()
End With
End If
AddHandler objPrintDoc.PrintPage, AddressOf
clsForm.clsPrint.objPrintDoc_PrintPage
Call objPrintDoc.Print()
End If
Catch objException As Exception
Call
clsExceptionHandler.gAddNewVarStatus("frmHostForm.Name", frmHostForm.Name)
Call
clsExceptionHandler.gAddNewVarStatus("blnShowPrnDialog", blnShowPrnDialog)
Call clsExceptionHandler.gLogException(objException,
"clsForm.clsPrint.Print")
' nothing to do
Finally
' nothing to do
End Try
End Sub
' >>>
Private Shared Sub objPrintDoc_PrintPage(ByVal sender As Object,
ByVal e As PrintPageEventArgs)
Try
Call e.Graphics.DrawImage(objFormImg, 0, 0)
Catch objException As Exception
Call clsExceptionHandler.gLogException(objException,
"clsForm.clsPrint.objPrintDoc_PrintPage")
' nothing to do
Finally
' nothing to do
End Try
End Sub
' >>>
Public Shared Function gCapture(ByRef frmHostForm As Form) As Bitmap
' Desc.: capture form and store image in bitmap
' Input: frmHostForm - (by reference) host form containing
control(s)
' Output: objFormImg - form image in bitmap
Dim objFormImg As Bitmap ' form image in bitmap
Dim objGraphics As Graphics ' graphics
Dim objFormSize As Size ' form size
Dim objMemGraphics As Graphics ' memory graphics
Dim objGraphicsDc As IntPtr ' handle to destination DC
for graphics
Dim objMemGraphicsDc As IntPtr ' handle to destination DC
for memory graphics
Try
objGraphics = frmHostForm.CreateGraphics()
objFormSize = frmHostForm.ClientSize
objFormImg = New Bitmap(objFormSize.Width, objFormSize.Height,
objGraphics)
objMemGraphics = Graphics.FromImage(objFormImg)
objGraphicsDc = objGraphics.GetHdc
objMemGraphicsDc = objMemGraphics.GetHdc
Call clsWin32Api.BitBlt(objMemGraphicsDc, 0, 0,
frmHostForm.ClientRectangle.Width, frmHostForm.ClientRectangle.Height,
objGraphicsDc, 0, 0, 13369376)
Call objGraphics.ReleaseHdc(objGraphicsDc)
Call objMemGraphics.ReleaseHdc(objMemGraphicsDc)
Catch objException As Exception
Call clsExceptionHandler.gAddNewVarStatus("frmHostForm.Name",
frmHostForm.Name)
Call clsExceptionHandler.gLogException(objException,
"clsForm.gVertCenterCtrl")
objFormImg = Nothing
Finally
' nothing to do
End Try
Return objFormImg
End Function
' >>> end of code >>>
"Jeffrey Tan[MSFT]" said:
Hi James,
Thanks very much for your feedback!
I have followed your steps to print the BitBlt result in 2 ways. But the
printing result on my side is the same. So I can not reproduce out this
issue. Can you paste some code snippet or send me a sample project to help
me reproduce it?
Also, I have viewed the saved bitmap of BitBlt in picture viewer, it is
almost the same as the form appears on the screen. I think there should not
but any problem about BitBlt method.
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.