Get the Exact Size of the Zoomed Image inside the Picturebox
Asked Answered
S

1

8

I'm using WinForms. In my Form i have a picturebox. It's sizemode is set to Zoom. If i load an image into the picturebox the image will zoom according to the picturebox dimensions.

I wanted to know how I can get the size of the zoomed image inside the picturebox.

These are some of the things i tried, but this doesn't give me the result i wanted.

    private void Debug_Write_Click(object sender, EventArgs e)
    {
        //var pictureWidth = pictureBox1.ClientSize.Width; //This gives the size of the picturebox
        //var picturewidth1 = pictureBox1.Image.Width; This gives the actual size of the image


        //var pictureHight = pictureBox1.ClientSize.Height; //This gives the size of the picturebox
        //var pictureHight1 = pictureBox1.ClientSize.Height; This gives the actual size of the image

        Debug.WriteLine("Width: " + pictureWidth.ToString() + " Height: " + pictureHight.ToString());
    }

Example: Actual Size of imageenter image description here

Picturebox size mode is set to Zoom, so it zoomed the image depending on my picturebox size:

enter image description here

How do i find out the zoomed length and width of this image?

Strephonn answered 31/12, 2015 at 21:38 Comment(3)
What do you mean by size of zoomed image inside picturebox? The real size of Image?Market
For Example, lets say my picturebox length: 1000 and Width is 500, and my image length: 2000 and width: 1000. When the image is loaded into the picturebox it will re-size it self accordingly. I want to know how to find the length and width of the image that has been re-sized to fit in the picturebox. @dotctorStrephonn
Zoom preserves the aspect ratio of the image, so you would have to do some calculations on which maximum dimension is used, width or height, and then calculate the other number accordingly.Silkaline
M
16

You should do the math!

Image img = //...;
PictureBox pb = //...;

var wfactor = (double)img.Width / pb.ClientSize.Width;
var hfactor = (double)img.Height / pb.ClientSize.Height;

var resizeFactor = Math.Max(wfactor, hfactor);
var imageSize = new Size((int)(img.Width / resizeFactor), (int)(img.Height / resizeFactor));
Market answered 31/12, 2015 at 22:0 Comment(6)
pb.ClientSize.Width;, in case the control uses borders. :-)Silkaline
@Silkaline Nice, added to answer.Market
Image img = pictureBox1.Image; PictureBox pb = new PictureBox(); did i do these 2 lines correctly?Strephonn
Image img = pictureBox1.Image; PictureBox pb = pictureBox1; @StrephonnMarket
Thank you for all your help! im still new to all of this :)Strephonn
You're answer is still helping to this day. I was getting stuck trying to draw on my picturebox and finally realised it was the zoom sizeable setting having the extra on the sides/top. Got it with this. Thanks.David

© 2022 - 2024 — McMap. All rights reserved.