如何在 C# windows 窗体中在可缩放图像上绘图

2023-12-19

所以我正在实现一个可以读取图像的项目,平移它,缩放它并做其他事情......一切都很顺利,直到我尝试用鼠标右键实现绘图。

问题是当我画一条线时,图像上出现的线与我在屏幕上画的线不对应,这意味着它发生了移动,并且我知道它是因为图像的大小和缩放而改变的,但是当我绘制时图像上的线条以其原始尺寸(图像)和平移;我没有问题。

这是代码。

首先这是我单击浏览并选择图像时加载图像的方式

Myimage = new Bitmap(ImagePath);
resized = myImage.Size;
imageResize();
pictureBox.Paint += new    System.Windows.Forms.PaintEventHandler(this.pictureBox_Paint);
                pictureBox.Invalidate();

imageResize 函数执行以下操作:

void imageResize()
{     
//calculated the size to fit the control i will draw the image on   
 resized.Height = someMath;
 resized.Width = someMath;
}

然后在 pictureBox_Paint 事件的事件处理程序中我写道:

private void pictureBox_Paint(object sender,      System.Windows.Forms.PaintEventArgs e)
{
// Create a local version of the graphics object for the PictureBox.
Graphics PboxGraphics = e.Graphics;
PboxGraphics.DrawImage(myImage, imageULcorner.X, imageULcorner.Y,     resized.Width, resized.Height);
}

正如您所看到的,调整后的尺寸不是原始图像尺寸,我这样做是因为我希望图像集中显示在图片框控件上并填充,现在下一部分是我的问题开始的地方

我必须使用鼠标右键在图像上画线,所以我实现了 pictureBox_MouseDown 和 pictureBox_MouseUp 事件处理程序

// mouse down event handler
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
else if (mouse.Button == MouseButtons.Right)
{
mouseDown = mouse.Location;
mouseDown.X = mouseDown.X - imageULcorner.X;
mouseDown.Y = mouseDown.Y - imageULcorner.Y;
draw = true;
}
}

这是鼠标按下事件处理程序

//Mouse UP
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
else if (mouse.Button == MouseButtons.Right)
{
if (draw)
 {
mouseLocationNow.X = mouse.X - imageULcorner.X;
mouseLocationNow.Y = mouse.Y - imageULcorner.Y;
//
// get graphics object of the image ( the original not the resized)
// as the resized image only appears when i draw on the graphics of the
// pictureBox control
// i know the problem lies here but how can i fix it
//
Graphics image = Graphics.FromImage(myImage);
Pen pen = new Pen(Color.Red, 2);
image.DrawLine(pen, mouseLocationNow, mouseDown);
pictureBox.Invalidate();
}
draw = false;
}

所以最后我希望能够在调整大小的图像上绘图并使其对应于真实图像以及我绘制线条的屏幕 感谢并抱歉发了这么长的帖子,但这个问题一直让我发疯。


这里有一个PictureBox子类不仅支持缩放功能Image还包括您在其表面上绘制的图形。

它包括一个SetZoom函数通过缩放自身和矩阵来放大。

它还有一个ScalePoint您可以使用函数从鼠标事件中收到的像素坐标计算未缩放的坐标。

这个想法是使用一个Transformation Matrix缩放任何像素Graphics对象将绘制在Paint event.

我为测试表单添加了一些代码。

public partial class ScaledPictureBox : PictureBox
{
    public Matrix ScaleM { get; set; }

    float Zoom { get; set; }
    Size ImgSize { get; set; }

    public ScaledPictureBox()
    {
        InitializeComponent();
        ScaleM = new Matrix();
        SizeMode = PictureBoxSizeMode.Zoom;
    }

    public void InitImage()
    {
        if (Image != null)
        {
            ImgSize = Image.Size;
            Size = ImgSize;
            SetZoom(100);
        }
    }

    public void SetZoom(float zoomfactor)
    {
        if (zoomfactor <= 0) throw new Exception("Zoom must be positive");
        float oldZoom = Zoom;
        Zoom = zoomfactor / 100f;
        ScaleM.Reset();
        ScaleM.Scale(Zoom , Zoom );
        if (ImgSize != Size.Empty) Size = new Size((int)(ImgSize.Width * Zoom), 
                                                   (int)(ImgSize.Height * Zoom));

    }

    public PointF ScalePoint(PointF pt)
    {   return new PointF(pt.X / Zoom , pt.Y / Zoom );     }

}

以下是表单中执行测试的代码:

public List<PointF> somePoints = new List<PointF>();

private void scaledPictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    somePoints.Add(scaledPictureBox1.ScalePoint(e.Location) );
    scaledPictureBox1.Invalidate();
}

private void scaledPictureBox1_Paint(object sender, PaintEventArgs e)
{
    // here we apply the scaling matrix to the graphics object:
    e.Graphics.MultiplyTransform(scaledPictureBox1.ScaleM);
    using (Pen pen = new Pen(Color.Red, 10f))
    {
        PointF center = new PointF(scaledPictureBox1.Width / 2f, 
                                   scaledPictureBox1.Height / 2f);
        center = scaledPictureBox1.ScalePoint(center);
        foreach (PointF pt in somePoints)
        {
            DrawPoint(e.Graphics, pt, pen);
            e.Graphics.DrawLine(Pens.Yellow, center, pt);
        }
    }
}

public void DrawPoint(Graphics G, PointF pt, Pen pen)
{
    using (SolidBrush brush = new SolidBrush(pen.Color))
    {
        float pw = pen.Width;
        float pr = pw / 2f;
        G.FillEllipse(brush, new RectangleF(pt.X - pr, pt.Y - pr, pw, pw));
    }
}

以下是绘制几个点后的结果,在四种不同的缩放设置下显示相同的点;这ScaledPictureBox显然被放置在AutoScroll-Panel。这些线条显示了如何使用常规绘图命令。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 C# windows 窗体中在可缩放图像上绘图 的相关文章