.NET Forum / Windows Forms / Drawing / November 2004
Need help on printing best-fit based on original aspect ratio
|
|
Thread rating:  |
Doug DeBug - 29 Nov 2004 02:22 GMT I would like to print an image as large as I can on any size of paper (user selected or default printer settings). The following code is based on Bob Powell's C# example. However, the image is too small
Help!!!!
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 'Load the image from file Dim _Image As Image = _Image.FromFile(ImageToPrint)
'Calculate the values needed for best-fit Dim PrintWidth As Single = CType(e.PageSettings.PaperSize.Width, Single) Dim PrintHeight As Single = CType(e.PageSettings.PaperSize.Height, Single) Dim WidthRatio As Double = _image.Width / PrintWidth Dim HeigthRatio As Double = _image.Height / PrintHeight Dim largestRatio As Double largestRatio = Math.Max(WidthRatio, HeigthRatio)
Dim posX As Single = CType((PrintWidth * largestRatio / 2 - _image.Width / 2), Single) Dim posY As Single = CType((PrintHeight * largestRatio / 2 - _Image.Height / 2), Single)
'Help me...I don't understand how the Matrix or Transform work. Dim mx As New Matrix(1.0F / CType(largestRatio, Single), 0, 0, 1.0F / CType(largestRatio, Single), 0, 0) mx.Translate(posX, posY) e.Graphics.Transform = mx ' Draw the image as large as you can but maintain aspect ratio of the image from file. e.Graphics.DrawImage(_image, 0, 0) e.HasMorePages = False End Sub
Thanks!!!
n_sp_m_doug.daly@scantiva.com
Bob Powell [MVP] - 29 Nov 2004 08:30 GMT I've tested this code on my Epson CX3650 and generally it works well. I think the page size needs to be adjusted to allow for a small border but otherwise it's *mostly* ok.
I have one picture that comes out too small though. I suspect it's due to a mis-declaration of sizes in the image itself. I'll investigate further.
 Signature Bob Powell [MVP] Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET. Subscribe to the RSS feeds provided and never miss a new article.
> I would like to print an image as large as I can on any size of paper > (user selected or default printer settings). The following code is [quoted text clipped - 38 lines] > > n_sp_m_doug.daly@scantiva.com Bob Powell [MVP] - 29 Nov 2004 08:46 GMT Ok, This has to do with the declared size of the image and the way that DrawImage interprets size and resolution. I fixed the problem by drawing in pixels only.
After my signature you'll find the amended routine.
 Signature Bob Powell [MVP] Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET. Subscribe to the RSS feeds provided and never miss a new article.
---------------------------------------------------------------------------- ----- Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
'Load the image from file
Dim _Image As Image = i
'Calculate the values needed for best-fit
Dim PrintWidth As Single = CType(e.PageSettings.PaperSize.Width, Single) ' / 100 * e.Graphics.DpiX
Dim PrintHeight As Single = CType(e.PageSettings.PaperSize.Height, Single) ' / 100 * e.Graphics.DpiY
Dim WidthRatio As Double = _Image.Width / PrintWidth
Dim HeigthRatio As Double = _Image.Height / PrintHeight
Dim largestRatio As Double
largestRatio = Math.Max(WidthRatio, HeigthRatio)
Dim posX As Single = CType((PrintWidth * largestRatio / 2 - _Image.Width / 2), Single)
Dim posY As Single = CType((PrintHeight * largestRatio / 2 - _Image.Height / 2), Single)
'Help me...I don't understand how the Matrix or Transform work.
Dim mx As New Matrix(1.0F / CType(largestRatio, Single), 0, 0, 1.0F / CType(largestRatio, Single), 0, 0)
mx.Translate(posX, posY)
e.Graphics.Transform = mx
' Draw the image as large as you can but maintain aspect ratio of the image from file.
e.Graphics.DrawImage(_Image, New Rectangle(0, 0, i.Width, i.Height), 0, 0, i.Width, i.Height, GraphicsUnit.Pixel)
e.HasMorePages = False
End Sub
---------------------------------------------------------------------------- -----
> I would like to print an image as large as I can on any size of paper > (user selected or default printer settings). The following code is [quoted text clipped - 38 lines] > > n_sp_m_doug.daly@scantiva.com Doug DeBug - 29 Nov 2004 16:29 GMT Here what I just tested:
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ Handles PrintDocument1.PrintPage 'Load the image from file Dim _Image As Image = _Image.FromFile(ImageToPrint)
'Calculate the values needed for best-fit Dim PrintWidth As Single = _ CType(e.PageSettings.PaperSize.Width, Single) ' /100 * e.Graphics.DpiX Dim PrintHeight As Single = _ CType(e.PageSettings.PaperSize.Height, Single) '/ 100 * e.Graphics.DpiY
Dim WidthRatio As Double = _Image.Width / PrintWidth Dim HeigthRatio As Double = _Image.Height / PrintHeight
Dim largestRatio As Double largestRatio = Math.Max(WidthRatio, HeigthRatio)
Dim posX As Single = CType((PrintWidth * _ largestRatio / 2 - _Image.Width / 2), Single) Dim posY As Single = CType((PrintHeight * _ largestRatio / 2 - _Image.Height / 2), Single)
'Use a Matrix to scale the image. Dim mx As New Matrix(1.0F / CType(largestRatio, Single), _ 0, 0, 1.0F / CType(largestRatio, Single), 0, 0) mx.Translate(posX, posY) e.Graphics.Transform = mx
' Draw the image as large as you can but 'maintain aspect ratio of the imagefrom file. e.Graphics.DrawImage(_Image, New Rectangle(0, 0, _Image.Width, _Image.Height), _ 0, 0, _Image.Width, _Image.Height, GraphicsUnit.Pixel) e.HasMorePages = False
End Sub
However, it does not keep the aspect ratio of the original image. You can test it by printing the same image in both landscape and portrait and comparing the output. I thought if you use a transform that the image would be drawn as "_Image, 0, 0" as it would be scaled and centered as is from the previous transform.
Doug
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|