如何使用C#撤消绘制操作 [英] how to undo the paint operation using c#
问题描述
我在picturebox.I加载的图像在图像上进行绘制操作,通过event.It油漆与黑色的小矩形区域的鼠标点击,当鼠标点击那个point.now我要实现的撤消操作此。当我点击一个按钮,最后绘制操作应该是undone.Here是我的$ C $下绘制操作。
I have a image loaded in picturebox.I perform a paint operation on the image, by the mouseclick event.It paints a small rectangular area with black color, when the mouse is clicked at that point.now I want to implement the undo operation for this .When i click a button the last paint operation should be undone.Here is my code for paint operation..
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
rect.Width = 0;
rect.Height = 0;
pictureBox1.Invalidate();
int radius = 10; //Set the number of pixel you want to use here
//Calculate the numbers based on radius
int x0 = Math.Max(e.X - (radius / 2), 0),
y0 = Math.Max(e.Y - (radius / 2), 0),
x1 = Math.Min(e.X + (radius / 2), pictureBox1.Width),
y1 = Math.Min(e.Y + (radius / 2), pictureBox1.Height);
Bitmap bm = pictureBox1.Image as Bitmap; //Get the bitmap (assuming it is stored that way)
for (int ix = x0; ix < x1; ix++)
{
for (int iy = y0; iy < y1; iy++)
{
bm.SetPixel(ix, iy, Color.Black); //Change the pixel color, maybe should be relative to bitmap
}
}
pictureBox1.Refresh(); //Force refresh
}
中的任何一个,请帮助我,我怎么可以撤消最后执行的操作。
Any one please help me how i can undo the last operation performed.
推荐答案
由于你使用光栅图像中的内存,你不能只是撤消操作。可以有多种解决方案,以这样的:
Because you're working with raster image in memory you cannot just undo the operation. There can be multiple solutions to this:
- 保留原始图象在存储器中,用于每个操作保持绘制参数:什么被吸引,在那里,其颜色。当你需要一个撤消你将不得不从第一重复所有的操作到最后(你也可以有你的地方存储中间图像的控制点)
- 每次操作后保留的图像快照 - 这将是非常存储器耗时。在撤销 - 恢复在列表中的previous图片 。
- 在保持变化像素 - 每个操作分析previous形象和新的,并保持像素的变化。您可以通过复制这些像素回恢复到previous。
这篇关于如何使用C#撤消绘制操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!