Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
Bitmap img;//outside current method.
                   private void Form1_Load(object sender, EventArgs e)
                   {
                    pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                    img = new Bitmap(file.FullName);
                    if (img.Height >= pictureBox1.Height )
                    {
                     //how do i change the img dimension?
                     //i want to Set the height or width of the loaded image, to fit it properly into my picturebox.
                     //if the image mode is vertical, my picturebox must have height> width. The height of the img will transform how high the picturebox high will be. 

                    }
                     pictureBox1.Image = img;
                   }
Posted
Updated 26-Jun-15 21:36pm
v2

I found this on a blog some years ago, but cannot find the original. This is my modified version of the blog. It's C++ but the principles are the same, converting it to C# should not be too difficult:
C++
// get the destination co-ordinates
::GetClientRect(hWnd, &rectWindow);

destRect.X = static_cast<real>(rectWindow.left);
destRect.Y = static_cast<real>(rectWindow.top);
destRect.Width = static_cast<real>((rectWindow.right - rectWindow.left));
destRect.Height = static_cast<real>((rectWindow.bottom - rectWindow.top));

/*
 * The key to keeping the correct aspect ratio while resizing an image is the algorithm
 * used to calculate the ratio, viz.
 *
 *	NewHeight = GivenWidth * (OriginalHeight / OriginalWidth)
 * or
 *
 *	NewWidth = GivenHeight * (OriginalWidth / OriginalHeight)
 *
 * This calculation assumes that the "Given..." is the dimension the image
 * should be resized to.
 * Once we know this, we can multiply it by the original image’s aspect,
 * and that will give us the other side's value we need. So, assuming the
 * original image has a width of 1000 and a height of 1600 and we want it
 * to be resized to a width of 500:
 *
 *	First find the aspect: (1600 / 1000) = aspect of 1.6
 *	Now multiply the aspect by the desired new width: 1.6 * 500
 *	The result of that multiplication is 800, which is what our height should be
 *	In other words:
 *	800 = 500 * (1600 / 1000)
 * So the resulting image would have a height of 800 and a width of 500.
 *
 */
imageWidth =  static_cast<real>(pImage->GetWidth());
imageHeight =  static_cast<real>(pImage->GetHeight());
// if width or height is less than window, use image size
if (imageWidth < destRect.Width)
    destRect.Width = imageWidth;
if (imageHeight < destRect.Height)
    destRect.Height = imageHeight;

if (imageWidth > destRect.Width)
{
    // if wider than window then adjust height by aspect ratio
    aspect = imageHeight / imageWidth;
    REAL newHeight = destRect.Width * aspect;
    // but don't make it deeper than the window
    if (newHeight <= destRect.Height)
        destRect.Height = newHeight;
}
if (imageHeight > destRect.Height)
{
    // if deeper than window then adjust width by aspect ratio
    aspect = imageWidth / imageHeight;
    destRect.Width = destRect.Height * aspect;
}
 
Share this answer
 
v2
it seem i got a stroke of geniality...:)
i used the three rule.
And is working perfectly... yuhoo.

C#
                    uc.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    img = new Bitmap(item.FullName);
                    uc.pictureBox1.Image = img;
                    if (img.Height >= uc.pictureBox1.Height)
                    {
                        //10/5 = 8/x  >> x= (8*5)/10 >> x=4   (10/5=2, 8/4=2)
                        uc.Height = (uc.pictureBox1.Width * img.Height) / img.Width;
                    }
// where uc = userControl that contain a public pictureBox1.
 
Share this answer
 
v2

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