Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing JPG to scale

Status
Not open for further replies.

Appollo14

Technical User
Joined
Sep 4, 2003
Messages
182
Location
GB
Hi,

I'm using vb.net 2003 and I have a form that uses a picture box to display an image. I have put a context menu on the picture box allowing the user to print the image, however on trying it I ended up with an image the size of a postage stamp in the corner of an A4 sheet. Could any one please advise me as to how to get the image to automatically scale to the size of the paper that is loaded in the printer?

Thanks in advance,
Noel.
 
Hi Prashant,

Thanks for the link, i think it's just what i'm after. I should now mention that i'm a bit (well maybe a lot) of a novice with programming so my next question may strike you as a bit stupid but please bear wth me if you will.

After reading thro the post i came up with this:

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim ImgX = Me.bxImageView.Image.Height
Dim ImgY = Me.bxImageView.Image.Width
Dim PSizeX = Me.PrintDocument1.DefaultPageSettings.PaperSize.Height
Dim PSizeY = Me.PrintDocument1.DefaultPageSettings.PaperSize.Width
Dim ScaleX = PSizeX / ImgX
Dim ScaleY = PSizeY / ImgY
e.Graphics.DrawImage(bxImageView.Image, ScaleX, ScaleY)
End Sub

which, as you will see is in the event handler and i'm coming up with this error when i try to run the process.

************** Exception Text **************
System.Reflection.AmbiguousMatchException: No accessible overloaded 'Graphics.DrawImage' can be called with these arguments without a narrowing conversion:
Public Sub DrawImage ( ByVal image As System.Drawing.Image, ByVal x As Single, ByVal y As Single )
Public Sub DrawImage ( ByVal image As System.Drawing.Image, ByVal x As Integer, ByVal y As Integer )
at System.Windows.Forms.PrintPreviewControl.CalculatePageInfo()
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()


************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
AddrdessAppNav
Assembly Version: 1.0.2460.14745
Win32 Version: 1.0.2460.14745
CodeBase: file:///C:/Documents%20and%20Settings/Administrator/My%20Documents/Visual%20Studio%20Projects/AddrdessAppNav/bin/AddrdessAppNav.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Data
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.data/1.0.5000.0__b77a5c561934e089/system.data.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 7.0.5000.0

What does it all mean? Am i implementing the code in the wrong place?

Many thanks,
Noel.
 
How did you initialize the variables?
DrawImage needs x and y either as single or integer...
So you must provide it as:
Dim ImgX as Integer...and so on...
and then do the ImgX = Me.bxImageView.Image.Height

This problem is causing due to this only...as there are two overrides for DrawImage which takes single or integer values for x and y...So provide the type appropriately and let me know...


Sharing the best from my side...

--Prashant--
 
Thanks Prashant,
that certainly got rid of that message. Now the next problem i've hit is that the image print or the print preview has not changed! I've tried stepping thro the code but my breakpoints indicate that they are not going to be hit - and they certainly arent.

I've put breakpoints as indicated with the (*)

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim ImgX As Integer
Dim ImgY As Integer
Dim PSizeX As Integer
Dim PSizeY As Integer
Dim ScaleX As Integer
Dim ScaleY As Integer
* ImgX = Me.bxImageView.Image.Height
* ImgY = Me.bxImageView.Image.Width
* PSizeX = Me.PrintDocument1.DefaultPageSettings.PaperSize.Height
* PSizeY = Me.PrintDocument1.DefaultPageSettings.PaperSize.Width
* ScaleX = PSizeX / ImgX
* ScaleY = PSizeY / ImgY
* e.Graphics.DrawImage(bxImageView.Image, ScaleX, ScaleY)


Thanks for your time.
Noel.
 
Hi Prashant,

thought i'd let you know that i'd realised my mistake and am very near to sorting it now.

I was not correctly getting the scale value and was puting the value that i did get in the margins value of the drawingimage.

This is the final (nearly) version of the code.

Dim ImgX As Integer
Dim ImgY As Integer
Dim PSizeX As Integer
Dim PSizeY As Integer
Dim ScaleX As Integer
Dim ScaleY As Integer
Dim RecX As Integer
Dim RecY As Integer
ImgX = Me.bxImageView.Image.Height
ImgY = Me.bxImageView.Image.Width
PSizeX = Me.PrintDocument1.DefaultPageSettings.PaperSize.Height
PSizeY = Me.PrintDocument1.DefaultPageSettings.PaperSize.Width
ScaleX = PSizeX / ImgX
ScaleY = PSizeY / ImgY
RecY = ImgY * ScaleX
RecX = ImgX * ScaleY
e.Graphics.DrawImage(bxImageView.Image, 0, 0, RecY, RecX)


Once again thanks for your help.

Noel.
 
Thats great...Thanx for the code. This will dafinately help others.

Sharing the best from my side...

--Prashant--
 
Hi, as i said that was the final (nearly) code.
When I got home I did some more tinkering about and found some improvements. Firstly, ScaleX and ScaleY had to be declared as doubles or there was an issue with this value being rounded and the resulting scaled image being way out.

Secondly, I had to introduce ScaleM and assign this the value of the lesser returned Scale values. This insures that the image will be in proportion AND that it will fit correctly onto the paper.

Lastly you will notice the IF statement that looks at the paper orientation to decide which values to use for height and width.

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim ImgX As Integer
Dim ImgY As Integer
Dim PSizeX As Integer
Dim PSizeY As Integer
Dim ScaleX As Double
Dim ScaleY As Double
Dim RecX As Integer
Dim RecY As Integer
Dim ScaleM As Double
Dim TopM As Integer
Dim LeftM As Integer

ImgX = Me.bxImageView.Image.Height
ImgY = Me.bxImageView.Image.Width
If Me.PrintDocument1.DefaultPageSettings.Landscape = False Then
PSizeX = Me.PrintDocument1.DefaultPageSettings.PaperSize.Height
PSizeY = Me.PrintDocument1.DefaultPageSettings.PaperSize.Width
Else
PSizeX = Me.PrintDocument1.DefaultPageSettings.PaperSize.Width
PSizeY = Me.PrintDocument1.DefaultPageSettings.PaperSize.Height
End If
ScaleX = PSizeX / ImgX
ScaleY = PSizeY / ImgY
If ScaleX < ScaleY Then
ScaleM = ScaleX
Else : ScaleM = ScaleY
End If
RecY = ImgY * ScaleM
RecX = ImgX * ScaleM
e.Graphics.DrawImage(bxImageView.Image, 0, 0, RecY, RecX)
'MessageBox.Show("IX " & ImgX & " IY " & ImgY & " PX " & PSizeX & " PY " & " SX " & ScaleX & " SY " & ScaleY & " SM " & ScaleM & " RX " & RecX & " RY " & RecY)
End Sub

*NB The message box is usefull for checking the returned values during debug.

Regards,
Noel.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top