c# - 在图片框内绘制矩形 SizeMode Zoom

标签 c# winforms zooming picturebox

我在 WindowsForms 项目中有一个图片框,其 SizeMode 为“缩放”。

我想在图像内绘制一个矩形,并获取其相对于图像而不是图片框的坐标。

问题是矩形的坐标与 Windows Paint 应用程序中选择的同一矩形不匹配。

这里是使用的代码:

  1. 开始绘画:

    /// <summary>
    /// Starts drawing.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        backupImage = pictureBox1.Image;
        _once = true;
        RectStartPoint = e.Location;
        pictureBox1.Invalidate();
    }
    
  2. 移动鼠标时:

    /// <summary>
    /// While moving mouse event, paint rectangle
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (_once) //Only draw rectangle while drawing mode
        {
            Point tempEndPoint = e.Location;
            Rect.Location = new Point(Math.Min(RectStartPoint.X, tempEndPoint.X),
                Math.Min(RectStartPoint.Y, tempEndPoint.Y));
    
            Rect = new Rectangle(
                Math.Min(tempEndPoint.X, Rect.Left),
                Math.Min(tempEndPoint.Y, Rect.Top),
                Math.Min(e.X - RectStartPoint.X, pictureBox1.ClientRectangle.Width - RectStartPoint.X),
                Math.Min(e.Y - RectStartPoint.Y, pictureBox1.ClientRectangle.Height - RectStartPoint.Y));
    
            pictureBox1.Refresh();
            pictureBox1.CreateGraphics().DrawRectangle(cropPen, Rect);
        }
    }
    
  3. 点击2次,完成绘制矩形:

    /// <summary>
    /// When mouse click is released, write in texbox the rectangle's coordinates.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (_once)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Point tempEndPoint = e.Location;
    
                _once = false;
                string sAux = string.Format("Left: {0}; Top: {1}; Width: {2}; Height: {3} \r\n", Math.Min(tempEndPoint.X, Rect.Left), Math.Min(tempEndPoint.Y, Rect.Top),
                        Math.Min(e.X - RectStartPoint.X, pictureBox1.ClientRectangle.Width - RectStartPoint.X), Math.Min(e.Y - RectStartPoint.Y, pictureBox1.ClientRectangle.Height - RectStartPoint.Y));
    
                textBox1.Text += sAux;
            }
        }
    }
    

结果是:

Windows 图像 Windows Image

绘制图像 enter image description here

正如您在两张图片上看到的,左侧、顶部、宽度和高度不匹配。

你能告诉我如何获得相同的结果吗?

Example2

最佳答案

这是一个帮助进行各种计算的函数:

void SetImageScale(PictureBox pbox, out RectangleF ImgArea, out float zoom)
{
    SizeF sp = pbox.ClientSize;
    SizeF si = pbox.Image.Size;
    float rp = sp.Width / sp.Height;   // calculate the ratios of
    float ri = si.Width / si.Height;   // pbox and image

    if (rp > ri)
    {
        zoom = 1f * sp.Height / si.Height;
        float width = si.Width * zoom;
        float left = (sp.Width - width) / 2;
        ImgArea = new RectangleF(left, 0, width, sp.Height);
    }
    else
    {
        zoom = 1f * sp.Width / si.Width;
        float height = si.Height * zoom;
        float top = (sp.Height - height) / 2;
        ImgArea = new RectangleF(0, top, sp.Width, height);
    }
}

下面是如何使用它,给定一个从鼠标坐标创建的矩形矩形:

float zoom = 1f;
RectangleF ImgArea = Rectangle.Empty;

SetImageScale(pictureBox1, out ImgArea, out zoom);

Point RLoc = Point.Round(new PointF( (Rect.X - ImgArea.X) / zoom, 
                                     (Rect.Y - ImgArea.Y) / zoom ));
Size RSz = Size.Round(new SizeF(Rect.Width / zoom, Rect.Height / zoom));

label1.Text =  "Selection in mouse coordinates: "  + Rect.ToString();
label2.Text =  "Selection in image coordinates: "  + new Rectangle(RLoc, RSz).ToString();

无论图像是横向还是纵向,或者图像或 PictureBox 的比例(如果有)更大,这都应该有效。

enter image description here

请注意,由于图像被强烈缩放,因此很难进行像素完美的选择。

该函数是 this post 中函数的变体.

关于c# - 在图片框内绘制矩形 SizeMode Zoom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50316403/

相关文章:

javascript - 如何禁用 GridView 中的 LinkBut​​ton

c# - PropertyGrid.BrowsableAttributes 有什么用?

vb.net - Propertygrid 中的复选框列表

c# - C#中的面板绘图放大

集合和迭代器的 C# 性能

c# - 多线程应用程序中的 SQLite "Database is locked"错误

c# - 在实体中正确创建导航类

C# HTTP 请求适用于控制台应用程序,但不适用于 WinForms

iphone - UIScrollView 缩放后如何重置?

css - 禁用 div 缩放,但允许缩放页面(备用 div)