鼠标悬停在图像上时绘画

时间:2021-06-03 20:22:43

I'm working on Window application based on .Net 2.0.

我正在开发基于.Net 2.0的Window应用程序。

I have used picture box to show an image. Use have ability of drawing lines on that image. Picturebox sizemode is set to zoom. Now the image opened in picture box is showing in center of picturebox with spaces around image.

我用图片框来显示图像。使用能够在该图像上绘制线条。 Picturebox sizemode设置为zoom。现在图片框中打开的图像显示在图片框的中心,图像周围有空格。

The code I'm having is able to paint on picturebox. But I want to restrict painting on picturebox. User should be able to paint only on image of picturebox.

我所拥有的代码能够在图片框上绘画。但我想限制绘图箱上的绘画。用户应该只能在图片框的图像上绘画。

Can you guide me in that context?

你能指导我吗?

1 个解决方案

#1


0  

This piece of code only writes into the image somewhere in the picturebox. Hope this is what you have in mind.

这段代码只写入图片框中的某个图像。希望这是你的想法。

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    var p = new Pen(Color.Red, 5f);
    var pbox = sender as PictureBox;
    var area = (Bitmap)pbox.Image;
    var g = Graphics.FromImage(area);

    g.DrawEllipse(p, e.X, e.Y, 5, 5);
    pbox.Image = area;

    p.Dispose();
    g.Dispose();
}

#1


0  

This piece of code only writes into the image somewhere in the picturebox. Hope this is what you have in mind.

这段代码只写入图片框中的某个图像。希望这是你的想法。

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    var p = new Pen(Color.Red, 5f);
    var pbox = sender as PictureBox;
    var area = (Bitmap)pbox.Image;
    var g = Graphics.FromImage(area);

    g.DrawEllipse(p, e.X, e.Y, 5, 5);
    pbox.Image = area;

    p.Dispose();
    g.Dispose();
}