Groups | Blog | Home
all groups > dotnet drawing api > november 2004 >

dotnet drawing api : Need help on printing best-fit based on original aspect ratio


n_sp_m_sales NO[at]SPAM scantiva.com
11/28/2004 6:22:53 PM
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_sales NO[at]SPAM scantiva.com
11/29/2004 8:29:39 AM
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.

Bob Powell [MVP]
11/29/2004 9:30:24 AM
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.

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





[quoted text, click to view]

Bob Powell [MVP]
11/29/2004 9:46:58 AM
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.

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

----------------------------------------------------------------------------
-----


[quoted text, click to view]

AddThis Social Bookmark Button