【问题标题】:Get the Exact Size of the Zoomed Image inside the Picturebox获取图片框内缩放图像的确切大小
【发布时间】:2015-12-31 21:38:21
【问题描述】:

我正在使用 WinForms。在我的表格中,我有一个picturebox。它的 sizemode 设置为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 尺寸缩放图像:

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

【问题讨论】:

  • 图片框内缩放图像的大小是什么意思?图片的实际尺寸?
  • 例如,假设我的 picturebox 长度:1000 和宽度是 500,我的图像长度:2000 和宽度:1000。当图像加载到 picturebox 时,它将重新相应地调整自己的大小。我想知道如何找到已重新调整大小以适合picturebox 的图像的长度和宽度。 @dotctor
  • Zoom 会保留图像的纵横比,因此您必须对使用的 最大 尺寸(宽度或高度)进行一些计算,然后相应地计算其他数字.

标签: c# .net image winforms picturebox


【解决方案1】:

你应该算算!

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

【讨论】:

  • pb.ClientSize.Width;,以防控件使用边框。 :-)
  • @LarsTech Nice,添加到答案中。
  • 图片 img = pictureBox1.Image;图片框 pb = 新图片框();这两行我做对了吗?
  • Image img = pictureBox1.Image; PictureBox pb = pictureBox1;@taji01
  • 感谢您的帮助!我对这一切还是陌生的:)
猜你喜欢
  • 2013-10-21
  • 2012-09-20
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
  • 2021-05-01
  • 2012-01-24
  • 2016-10-31
  • 1970-01-01
相关资源
最近更新 更多