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

1 c# graphics line zooming winforms

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

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

这是代码。

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

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

imageResize 函数执行以下操作:

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

然后在 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);
}
Run Code Online (Sandbox Code Playgroud)

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

我必须使用鼠标右键在图像上画线,所以我实现了 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;
}
}
Run Code Online (Sandbox Code Playgroud)

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

//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;
}
Run Code Online (Sandbox Code Playgroud)

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

TaW*_*TaW 5

这是一个PictureBox子类,它不仅支持将缩放应用到Image您在其表面上绘制的图形。

它包括SetZoom通过缩放自身和矩阵来放大的功能。

它还具有一个ScalePoint可用于根据鼠标事件中收到的像素坐标计算未缩放坐标的函数。

这个想法是使用 aTransformation Matrix来缩放Graphics对象在事件中绘制的任何像素Paint

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

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

}
Run Code Online (Sandbox Code Playgroud)

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

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述在此输入图像描述在此输入图像描述在此输入图像描述