Hi
I'm unsuccessfully trying to print a form's image under
VB.NET. To print under .NET is a real pain in the [...], I
find it to be complex, lenghty, confusing, upsetting and
ultimately not to be working at all.
My strategy is first to save the form's image as a bitmap
(this works, I can save the image to a file). Then I use
the printer's Graphics object's drawImage method to send it
to the printer. Most of the time a blank paper comes out;
when something finally get printed (depending on which
printer I'm using), it's too small to be useful.
Sometimes, the printer's resolution is a negative value
(???). Sometimes I must specify margins in tenth of
millimeters instead than in hundreth of an inch. And the
list of anomalies, annoyances and inconsistences continues...
I'm wondering, am I alone in finding that Microsoft really
goofed when it designed to .NET's printing architecture?
Really makes me regret working with Java.
If anyone can tell me why my code produces such
inconsistent documents, it would really be appreciated. I
can still save the image and then use Paint (or whatever
else) to print the image, but that really looks
unprofessionnal.
Here's my code (but not all of it, as you may understand!):
' Class attributes
Private WithEvents doc As PrintDocument
Private form_image As Bitmap ' Image to be printed
Private print_d As PrintDialog
Private page_sd As PageSetupDialog
[...]
' The PrintDocument and dialog boxes are initialized in
another method; I won't list it here for clarity
Private Sub doc_PrintPage(ByVal sender As Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles
doc.PrintPage
Dim g As Graphics = e.Graphics
' Image size (pixels)
Dim xImagePixels As Integer = form_image.Width
Dim yImagePixels As Integer = form_image.Height
Dim srcRect As New Rectangle(0, 0, xImagePixels,
yImagePixels)
Dim ps As PageSettings = doc.DefaultPageSettings
' Paper size (inch)
Dim pgX As Single = ps.PaperSize.Width * 0.01
Dim pgY As Single = ps.PaperSize.Height * 0.01
' page margins (inch)
Dim mLeft As Single = ps.Margins.Left * 0.01
Dim mRight As Single = ps.Margins.Right * 0.01
Dim mTop As Single = ps.Margins.Top * 0.01
Dim mBottom As Single = ps.Margins.Bottom * 0.01
' Printer resolution (dot per inch)
Dim rx As Single = ps.PrinterResolution.X
Dim ry As Single = ps.PrinterResolution.Y
' Dimensions of the image to be printed - in pixels
' using the specified printer's resolution
Dim destRec As New Rectangle(0, 0, (pgX - mLeft - mRight)
* rx, (pgY - mTop - mBottom) * ry)
' Draw the image to the printer - should work???
g.DrawImage(form_image, srcRect, destRec, GraphicsUnit.Pixel)
End Sub
So that's it. I'm really suspicious about the
PrinterResolution property; is it really meaningful? What
else could I do to know the scaling factor I must use to
transform the original image to its new size?
Any help (or sharing of similar problems) may be useful.
JF