简体   繁体   English

获取Picturebox内缩放图像的精确大小

[英]Get the Exact Size of the Zoomed Image inside the Picturebox

I'm using WinForms. 我正在使用WinForms。 In my Form i have a picturebox . 在我的表格中我有一个图片picturebox It's sizemode is set to Zoom . 它的sizemode设置为Zoom If i load an image into the picturebox the image will zoom according to the picturebox dimensions. 如果我将图像加载到图片picturebox ,图像将根据图片picturebox尺寸进行缩放。

I wanted to know how I can get the size of the zoomed image inside the picturebox . 我想知道如何在图片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 image 示例:图像的实际大小 在此输入图像描述

Picturebox size mode is set to Zoom , so it zoomed the image depending on my picturebox size: Picturebox的大小模式设置为Zoom ,所以放大根据我的图像picturebox大小:

在此输入图像描述

How do i find out the zoomed length and width of this image? 如何找到此图像的缩放长度和宽度?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM