4

In my app (winforms), I'd like to load in different images. These images have different sizes, and different aspect ratios (let's say I have a 400x400, and a 1920x1200.) Now I have a picturebox to put these images in, and the picturebox's SizeMode property is set to Zoom.

Now I have an image in a picturebox, that is resized to fit within the picuterbox's boundaries. But if the picturebox has a different aspect ratio than the image has, I will be left with some unwanted empty space. Setting the SizeMode to stretch, is not an option.

?: So I'd like to know, if there is a way to get the size of the auto-resized image, so I can change the size of the picturebox accordingly.

Image myImg = new Image.FromFile(..//landscape.jpg)
int getWidth = myImg.Width;
int getHeight = myImg.Height;
// This does not work, as it gets the original size of the image (eg: in case of a 1920x1200, it gets 1920 and 1200 respectively)

This is what happens right now:

Wrong ratio Wrong ratio

This is what I'd like to have:

Required ratio Because the app should be able to process any image, I need to set these values dynamically, so no values can be preset.

zergerge
  • 179
  • 2
  • 3
  • 13
  • 2
    could you not just set sizemode to auto? –  Oct 15 '14 at 08:14
  • something like [PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;](http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.sizemode(v=vs.110).aspx) –  Oct 15 '14 at 08:15
  • If I set it to auto, displays its native size, which in case of a large resolution image means that you get an incredibly large display of the image. About stretchimage: I told you it is not an option as I need to maintain the aspect ratio. – zergerge Oct 15 '14 at 08:20
  • have a look at [this](http://stackoverflow.com/questions/3075906/using-c-sharp-how-can-i-resize-a-jpeg-image) question –  Oct 15 '14 at 08:24
  • I don't want to resize my image. That is a different question. I want to keep the original resolution of the image, but display it smaller. It is essential, because I have a zoom function, and if I resize the image, zooming in will have a pixelated and blurry result. – zergerge Oct 15 '14 at 08:29

1 Answers1

4

Let us say the box is 400x400. When a picture goes into the box, it is resized to fit within the boxes boundaries but keeps it's aspect ratio. So what we need to do is calculate the new size of the image within the box, and resize the box to match.

Image myImg = new Image.FromFile(..//landscape.jpg)
int getWidth = myImg.Width;
int getHeight = myImg.Height;
double ratio = 0;
if(getWidth>getHeight)
{
    ratio = getWidth/400;
    getWidth=400;
    getHeight=(int)(getHeight/ratio);
}
else
{
    ratio = getHeight/400;
    getHeight=400;
    getWidth=(int)(getWidth/ratio);
}
pictureBox.Width=getWidth;
pictureBox.Height=getHeight;

(Don't know the exact classes so might throw an error or two, concept is sound though)

James Hunt
  • 2,448
  • 11
  • 23
  • I have been thinking about this myself, the only problem is that if I calculate the aspect ratio it is going to be inaccurate, because of possible fractured decimals. But I might stick with this solution, but I rather have something more accurate :) – zergerge Oct 15 '14 at 08:23
  • If you round the values, the greatest inaccuracy would be 1px. Depending on the size of the picture box 1px is usually acceptable. – James Hunt Oct 15 '14 at 08:28
  • Alright, you got me. I will give it a try. Thank you. :) – zergerge Oct 15 '14 at 08:34