Question about printing

J

James Wong

Dear all,

I'm new in printing in VB.NET 2003 and want to print a run-time generated
form which contains line drawing shape and text. I found that PrintDocument
is good for printing by code but not for entire form. Actually it is not a
kind of report but label sticker so I don't think Crystal Report is my
choice. Does anybody have idea on my issue?

Thanks for your attention and kindly help!

Regards,
James Wong
 
J

James Wong

Hi Sijin,

Thanks a lot for your information!

But I'm wondering how complicated to just print a form! I think I need some
time to catch and understand the technic inside.

Regards,
James Wong
 
J

James Wong

Hi Sijin,

Some more question about the form printing.

According to one of the articles you suggest, ControlPaint class is used to
print form. However, if there is some line drawing whic is not control, how
should I handle it? And, if the form contains some self-defined control or
third-party commercial control (like barcode or something else), how should
I handle it?

My goal is getting a generic purpose solution to print what are on the form,
just like a "Print-Screen" feature.

Thanks again for your kindly help!

Regards,
James Wong
 
J

Jeffrey Tan[MSFT]

Hi James,

Thanks for your feedback!

ControlPaint can only be used to draw common control, other customized
control and graphics can not be drawed with this class. The article "How
To: Printing Form Controls in C# and .NET" is on the assumption of we are
the developer of the application, so we may provide a function to draw the
lines and controls on the form to the printer Device Context.

For your requirement of printing some third party control etc, because we
are not the author of the control, we can not do the internal printing. We
have to use the print screen way, that is copying every bit of the
application to the printing DC.

To copy every bit, we should p/invoke WIN32 API BitBlt, for more
information, please refer to:
"Screen Capturing a Form in .NET - Using GDI and GDI+"
http://www.c-sharpcorner.com/Graphics/ScreenCaptFormMG.asp

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.
 
J

James Wong

Hi Jeffrey,

Thanks for your reply and information!

I got an article from MSDN talking about form print using VB.NET (no article
number on that article, the URL is
http://msdn.microsoft.com/library/cht/dv_vbcode/html/vbskcodeexampleprintingform.asp).
On the article, it also introduces BitBlt can handle the case. However, on
that article, the definition of BitBlt is

Private Declare Function BitBlt Lib "gdi32.dll" Alias "BitBlt" (ByVal _
hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As _
Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal _
hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, _
ByVal dwRop As System.Int32) As Long

It is slightly different from the definition quoted on the article you
suggest:

private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);

Do they have same function except the languge?

Besides, the MSDN article also states that I should have the right to use
unmanaged code to run the example. I don't know how I konw if I have such
right, or how to get the right to do that. Moreover, I also don't know if
there is any drawback to open right to use unmanaged code.

Sorry for my long question and hope you can help me again!

Regards,
James Wong

"Jeffrey Tan[MSFT]" said:
Hi James,

Thanks for your feedback!

ControlPaint can only be used to draw common control, other customized
control and graphics can not be drawed with this class. The article "How
To: Printing Form Controls in C# and .NET" is on the assumption of we are
the developer of the application, so we may provide a function to draw the
lines and controls on the form to the printer Device Context.

For your requirement of printing some third party control etc, because we
are not the author of the control, we can not do the internal printing. We
have to use the print screen way, that is copying every bit of the
application to the printing DC.

To copy every bit, we should p/invoke WIN32 API BitBlt, for more
information, please refer to:
"Screen Capturing a Form in .NET - Using GDI and GDI+"
http://www.c-sharpcorner.com/Graphics/ScreenCaptFormMG.asp

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.
 
J

James Wong

Hi Jeffrey,

I can print the form by using BitBlt successfully. However, I found the
print-out is not sharp enough to meet a production requirement. Do you
think it is the nature of "print-screen" or something else? I'm using HP
laster printer 2100 with 600 dpi.

Thanks for your attention and kindly advice!

Regards,
James Wong
 
J

Jeffrey Tan[MSFT]

Hi James,

Thanks very much for your feedback!

Can you tell me why you did not like this way of printing? Actually, this
is the standard way of doing print screen. The BitBlt win32 API will
perform a bit-block transfer of the color data corresponding to a rectangle
of pixels from the specified source device context into a destination
device context. The result data to the printer should be the same as they
are on the machine, and it should not be any different.

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.
 
J

Jeffrey Tan[MSFT]

Hi James,

Actually, there is not much difference between these 2 declaration, except
the return value type: one is bool type, another is long type. But both of
these 2 types will work for p/invoke for win32 bool type.

Because we did not use the return value, we need not care about this little
difference.

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.
 
J

James Wong

Hi Jeffrey,

Thanks again for your reply!

On my first trial to print, I use BitBlt to capture the form and store a
Bitmap object, then use DrawImage to print the Bitmap directly. The
printout is blur so I feel the quality is poor to be a commercial
application.

On my next trial, I save the Bitmap into a bitmap file first. Then load the
bitmap file into an Image object, then use DrawImage to print. The printout
is much better.

Do you have any idea on this issue?

Thanks for your kindly attention!

Regards,
James Wong
 
J

Jeffrey Tan[MSFT]

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.
 
J

James Wong

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.
 
J

James Wong

Hi Jeffrey,

Thanks for your following-up!

Things are quite strange on my side. After I prepare a simple demo printing
for your reference, the printout becomes normal. And my original testing
program prints normal, too. So I can't show you what I was facing last
week. But anyway, thanks for your help and will let you know if I get
another printing problem.

Regards,
James
 
J

Jeffrey Tan[MSFT]

Hi James,

Thanks very much for your feeback!!

Ok, if your problem arise again, please feel free to feedback. Thanks

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top