擦除在PictureBox中的图像上的线条,这些线条是通过鼠标单击绘制的
问题描述:
你好朋友,
我正在使用图片框中的图像上的(DrawLines)绘制线,我可以使用下面的代码绘制线,,但是我想当用户单击按钮(在图片框下提供)时,用户在图像上绘制的所有线应该被删除.
谢谢,,,,
Hello friends,,
I am working on drawing Lines using (DrawLines) over Image in picturebox,I am able to draw lines using below code,,But i want when user click on Button (provided below picture box) all the lines which user have drawn over the image should be Erased .
Thanks,,,,,
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pointArray[iNumberofClicks].X = e.X;
pointArray[iNumberofClicks].Y = e.Y;
Pen PenSpikes = new Pen(Color.Green);
SolidBrush solidBrush = new SolidBrush(Color.Blue);
if(iNumberofClicks==0)
{
a = e.X;
b = e.Y;
}
iNumberofClicks++;
if (iNumberofClicks > 1)
{
Point[] CurrentpointArray = new Point[iNumberofClicks];
for (int aa = 0; aa < iNumberofClicks; aa++)
{
CurrentpointArray[aa].X = pointArray[aa].X;
CurrentpointArray[aa].Y = pointArray[aa].Y;
}
Graphics offScreenDC = Graphics.FromImage(pictureBox1.Image);
offScreenDC.DrawLines(PenSpikes, CurrentpointArray);
offScreenDC.Dispose();
pictureBox1.Refresh();
}
}
for (int j = 1; j < CurrentpointArray.Length;j++ )
{
if ((Convert.ToInt16(CurrentpointArray[j].X) == a && Convert.ToInt16(CurrentpointArray[j].Y) == b) )
{
MessageBox.Show("alert");
break;
}
}
答
在绘制第一行之前,需要将原始图像缓存在内存中.像这样的东西:
Before you draw the first line you need to cache the original image in memory. Something like this:
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms)
然后,当您要清除线条时,请重新加载原始图像.同样是这样的:
Then when you want to clear the lines you reload the original image. Again something like this:
Image img = new Image(ms);
pictureBox1.Image = img;