Resize Image to fit on paper for print

gcollins

Guest
Joined
Sep 13, 2006
Messages
31
Location
Port Hope
Programming Experience
Beginner
Hi,

Newbie here, I got my print function working so that it will take an image from a picture box and print it. But the problem I am having is that the original image is larger than the paper size.

I found this thread http://www.vbdotnetforums.com/showthread.php?t=12605&highlight=printing

JohnH says:

"Calculate the scale you want limited to the e.MarginBounds of the paper. MarginBounds is the printing area within margins settings, and this vary according to what paper size and orientation (landscape/portrait) user chooses to print on."

Do I do this in the print page event? If so I am not sure how the code should go.

So basically how do I scale my image down to fit within the margins of the page?

G
 
When I print an image I use a rectangle that has a defined size
like beow

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Name [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Application.Info.DirectoryPath & [/SIZE][SIZE=2][COLOR=#800000]"\MyMotherInLaw.wmf"[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] newImage [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Image = Image.FromFile(Name)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MotherInLawRect [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Rectangle(MyLeft, MyTop , 210, 50)[/SIZE]
[SIZE=2]g.DrawImage(newImage, MotherInLawRect)[/SIZE]

The 210, 50 means that she is a lot wider than tall.
 
But I need to adjust my image to the page layout (Portrait/Landscape) so how would I find out the margins of the page layout to determine what size rectangle to use, and resize proportionally? or am I way off?

Sorry to hear about your MotherInLaw, LOL
 
Im new to this but I did find

VB.NET:
Dim margX AsSingle = Me.PrintDocument1.PrinterSettings.DefaultPageSettings.HardMarginX
 
Last edited by a moderator:
Scaling Formula, anybody?

I am trying to scale an image does, anyone know the formula to do this.

Say I have an image 993 x 730 I need to fit the width into an area of 800, proportionally the image should end up being 800 x 588 (figured that out in a graphics program).

What would the formula be to scale both the width and height so that they are in proportion?

Probably a no brainer but I can't seem to figure this out.
 
Mmm, maybe you want a bit more control over how this is done than the way i'm about to show you. Never-the-less the DrawImage method of the graphics class is seriously overloaded. One overload in particular accepts a rectangle as an argument, when you pass this rectangle any image passed in, in the first argument is scaled to fit the rectangle. Ex..


VB.NET:
e.Graphics.DrawImage(SomeImage, New Rectangle(0,0,50,50))

So event if your image is much bigger that 50x50 the graphics class does you a big favour and scales the image for you, in this case down to 50x50
 
Thanks,

I was trying to resize my image for printing so what I did was:

1. Get Print area width
2. Get image width
3. PrintAreaWidth / ImageWidth = ScaleFactor
4. RectangleWidth = ImageWidth * ScaleFactor
5. RectangleHeight = ImageHeight * ScaleFactor
6. Dim printArea As New Rectangle(x, y, RectangleWidth, RectangleHeight)
7. e.Graphics.DrawImage(pict_imageDisplay.Image, printArea)

Now it resizes any image to large for the print are proportionally and works great.

Not sure if this is the best way to do this but works for me, LOL.

Thanks again for your help
 
vis781, that won't scale proportionally.

gcollins, you only scale by one dimension (widths), try again. You must calculate the scales of both widths and heights and use the largest of the scales. Only then you got a method that will always fit a bigger rectangle into a smaller one.
 
I never said it would scale proportionally. I just tried to provide a simple way to scale an image, which would be the hard part if you tried to do it manually, the math, from that point onward is relatively simple.

Besides gcollins example will work just fine if the image is a perfect square, unfortunately it isn't in this case. So JohnH is correct. You will need to create a scale factor for the width and the height.

gcollins in this instance it maybe useful to use the inflate method.

VB.NET:
Rectangle.Inflate(ScaleWidth,ScaleHeight)
Or to reduce

VB.NET:
Rectangle.Inflate(-ScaleWidth,-ScaleHeight)
 
My image is 993x730 the area I am trying to put it into is 800x1067 (print area on paper, portrait)

If I get the scale factor using 800/993 = .8056394....

993 * .8056394... = 800

How come that number does not work for the height?

730 * .8056394... = 588.116817...

When I resize in my graphics program 993x730 to 800 wide I get a height of 588.

Is this not Right?
 
If your image was opposite h993 w730 that would give your scale (800/730=) 1.096 and new image size h1088 w800, which would make it too large to fit into the container (h1088>h1067). Now if you also calculated width scale (1067/993=) 1.075 and used this lesser scale you get new image size h1067 w784 that would fit.

You calculate opposite of my suggestion, but (new/old)[lesser]*dimension is the same as dimension/(old/new)[larger], so it's still the same conclusion - you must calculate both and multiply the lesser scale, I calculate both and divide the larger scale. A two-dimensional image got two dimensions and you have to treat it accordingly.

If you only want to adjust to one dimension (for instance width) and don't care if that makes your image to tall to fit the container that is ok. My suggestion was for scaling an image to always fit the requested container.
 
Sweet, that makes sense now, thanks alot.

I never took into account that if I resize an image to fit the width then there is still the possibility that the height could still be larger than the container.

Thanks John much appreciated for the help and understanding of resizing images.

G
 
Thanks,

I was trying to resize my image for printing so what I did was:

1. Get Print area width
2. Get image width
3. PrintAreaWidth / ImageWidth = ScaleFactor
4. RectangleWidth = ImageWidth * ScaleFactor
5. RectangleHeight = ImageHeight * ScaleFactor
6. Dim printArea AsNew Rectangle(x, y, RectangleWidth, RectangleHeight)
7. e.Graphics.DrawImage(pict_imageDisplay.Image, printArea)

Now it resizes any image to large for the print are proportionally and works great.

Not sure if this is the best way to do this but works for me, LOL.

Thanks again for your help

You help me a lot with this, it works for me too.
Thank you!
 
Back
Top