图片框画线在单击按钮后被删除. [英] picturebox drawlines are remove in button click.

查看:88
本文介绍了图片框画线在单击按钮后被删除.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,


我在图片框中有用于画图线的代码,并且我希望在单击按钮时删除所有线条,并且在用户单击鼠标后,这些线条不会显示.

谢谢

dear friends,


i have this code for drawlines in picturebox and i want that all lines are remove in button click , and after user mouse click then these lines are not show.

thanks

int iNumberofClicks = 0;
        Point[] pointArray = new Point[100];

 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++ )
                {
                    //int d=(Convert.ToInt16(CurrentpointArray[j].X));
                    //int f=Convert.ToInt16(CurrentpointArray[j].Y);
                    if (Convert.ToInt16(CurrentpointArray[j].X) == a  && Convert.ToInt16(CurrentpointArray[j].Y) == b  )
                    {
                        MessageBox.Show("alert");
                            break;
                    }
                }
                Array.Clear(CurrentpointArray,0,CurrentpointArray.Length);
            }
        }

推荐答案

问题是您直接在图像上绘制:
The problem is that you are drawing directly on the image:
Graphics offScreenDC = Graphics.FromImage(pictureBox1.Image);

这意味着直接修改PictureBox控件中保存的Image. 撤消"这些更改的唯一方法是从原始源重新加载图像.

顺便说一句:如果您按照上述方式构造一个Graphics对象,则您有责任在其上调用Dispose-如果您不这样做,则由于Graphics上下文是稀缺资源,因此程序将无法预料地崩溃.

就个人而言,我将处理PictureBox Paint事件,并使用提供的Graphics上下文在那里绘制所有必需的线,而不是直接在Image上绘制(除非您打算以后保存Image,即使那样我也将立即应用更改)在我写文件之前).

Which means that the Image held in the PictureBox control is modified directly. The only way to "undo" those changes is to reload the image from the original source.

BTW: If you construct a Graphics object as above, then you are responsible for calling Dispose on it - if you don''t, then your program will crash unpredictably, as Graphics contexts are scarce resource.

Personally, I would handle the PictureBox Paint event, and draw all the required lines there using the Graphics context supplied, rather than drawing on the Image directly (unless you intend to save the Image afterwards, and even then I would just apply the changes immediately before I wrote the file).


这篇关于图片框画线在单击按钮后被删除.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆