面板控件上清除的图形创建无效

时间:2018-12-10 10:02:39

标签: c# winforms graphics panel

我有一个寡妇形式的小组。我正在通过创建图形来创建图形,如以下代码所示

private void pnlDraw_MouseMove(object sender, MouseEventArgs e)
        {

            if (startPaint)
            {
                using (g = Graphics.FromImage(bmp))
                {
                    if ( x != -1 && y != -1) 
                    {

                        Color mynewcolor = Color.FromName(myColor);
                        Pen p = new Pen(Color.Black, 5);
                        g.DrawLine(p, new Point(x, y), e.Location);
                        x = e.X;
                        y = e.Y;
                    }
                }
                pnlDraw.Invalidate();                    
            }
        }

 private void pnlDraw_Paint(object sender, PaintEventArgs e)
        {

            e.Graphics.DrawImage(bmp, Point.Empty);
        }

我正在尝试清除面板,(在这样的按钮单击事件中清除我在面板上绘制的内容。)

但这不起作用吗?我应该怎么做才能清除面板?

private void btnClear_Click(object sender, EventArgs e)
        {
            pnlDraw.Invalidate();
        }

2 个答案:

答案 0 :(得分:1)

使用LarsTech的答案

(Graphics g = Graphics.FromImage(bmp)) { g.Clear(pnlDraw.BackColor); }
pnlDraw.Invalidate();
pnlDraw.Update();

答案 1 :(得分:-1)

尝试一下:

private void btnClear_Click(object sender, EventArgs e)
    {
        pnlDraw.Invalidate();
        pnlDraw.Update();
    }