Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi Guys,

I have a C# WinForms application where I need to display a series of images one at a time. The original images are all 512w x 768h and I need to stretch them to display at the maximum height the computer screen will allow while maintaining the aspect ratio (1 to 1.5). I Dock the PictureBox to the right side of the form which fixes the height.

How do I compute the width? I can't simply multiply the width by a factor of original height and new height. When I do this, images will display fine on some computers but will be distorted in height or width on others.

Here's what I'm doing now:
C#
int newHeight = pbImage.ClientRectangle.Height;
double multiplier = (newHeight / 768D);
int newWidth = ((int)Math.Ceiling(512 * multiplier));


As I said, this works on some but not all computers. Is there a solution?

Thanks....
Posted
Updated 1-Oct-12 4:58am
v2
Comments
Stephen Hewison 1-Oct-12 10:56am    
Doesn't the picture box have either a stretch or a zoom mode which does this automatically?
Steve Harp 1-Oct-12 11:01am    
SizeMode=Zoom doesn't work as documented on all computers. Some computers will show the images stretched or compressed in width distorting the display.
Steve Harp 1-Oct-12 11:13am    
Also, Setting SizeMode tells the PictureBox to size or position the image for you. This prevents me from adjusting the height. I must set SizeMode=Normal so I can make the image as tall as possible; I then need to compute the width so as not to distort.
Stephen Hewison 1-Oct-12 11:15am    
Oh wait, I think I see your problem. Solution coming.
Stephen Hewison 1-Oct-12 11:21am    
No I was wrong. Thought (newHeight / 768D) would give an int because newHeight is an int.

1 solution

This is all about aspect ratio:
http://en.wikipedia.org/wiki/Aspect_ratio[^].

This is hard-coded aspect ratio, so this code is not useful at all. You need to use:
C#
//having
PictireBox myPictireBox = //...

//...

//you can find the required aspect ratio using
int width = myPictireBox.Image.Width;
int height = myPictireBox.Image.Height;

//and from here, you can adjust myPictireBox.Width or myPictireBox.Height accordingly


Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.image.aspx[^].

This sounds like a legitimate use of the class PictureBox, but also consider not using it. This class is often heavily abuses, and, if by any chance you decide to get just a bit more complex rendering or behavior, this class will be turned a hassle rather then help. It could be much easier to render your images and graphics immediately on the surface of the control you presently use as a parent of your picture box, so a picture box is just a dumb middleman you don't really need. Please see my past answers on related topics:

How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^];

and also:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

Good luck,
—SA
 
Share this answer
 
v2
Comments
Stephen Hewison 1-Oct-12 14:41pm    
+5
Sergey Alexandrovich Kryukov 1-Oct-12 14:44pm    
Thank you, Stephen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900