获取 Picturebox 中缩放图像的确切大小

Get the Exact Size of the Zoomed Image inside the Picturebox

我正在使用 WinForms。在我的表格中,我有一个 picturebox。它的大小模式设置为 Zoom。如果我将图像加载到 picturebox 中,图像将根据 picturebox 尺寸进行缩放。

我想知道如何在 picturebox 中获取缩放图像的大小。

这些是我尝试过的一些方法,但这并没有给我想要的结果。

    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());
    }

示例: 图片的实际大小

Picturebox 大小模式设置为 Zoom,因此它根据我的 picturebox 大小缩放图像:

如何找出这张图片的缩放长度和宽度?

你应该算一下!

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));