如何删除图片框上所有绘制的矩形? (不在图像上)

时间:2018-11-29 13:12:27

标签: c# picturebox system.drawing drawrectangle

我尝试使用以下代码:

    this.parametrosMensagemService.setMessage(this.alert)
    this.pagerService.items = this.pagerService.items.filter(x => x.selecionado == "checked")
    this.setPage(1)

但是它什么也没做。我想清除图片框上所有绘制的图形,因为单击了下一页后,我想在其上绘制新的矩形。因此,请不要向我提供这样的解决方案:

        pictureBox1.Invalidate(); 
        //or
        pictureBox1.Update();
        //or
        Refresh();

g.FillRectangle(Brushes.Black, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height))

有人想与我分享解决方案吗?我对此表示赞赏。

page 2 page 3

Form2.cs

Graphics.Clear();

UserRect.cs

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Pen pen = new Pen(Color.Red, 2);
        e.Graphics.DrawRectangle(pen, rect);     
    }


    bool draw;
    int x_start, y_start;
    Rectangle rect;
    //UserRect rect;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {

        }
        else if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            pictureBox1.Cursor = Cursors.Cross;
            draw = true;
            x_start = e.X;
            y_start = e.Y;
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {


        }else if(e.Button == MouseButtons.Right)
        {
            pictureBox1.Cursor = Cursors.Cross;

        }

        if (!draw) return;
        LocalMousePosition = pictureBox1.PointToClient(Cursor.Position);

        int x = Math.Min(x_start, LocalMousePosition.X);
        int y = Math.Min(y_start, LocalMousePosition.Y);

        int width = Math.Max(x_start, LocalMousePosition.X) - Math.Min(x_start, LocalMousePosition.X);
        int height = Math.Max(y_start, LocalMousePosition.Y) - Math.Min(y_start, LocalMousePosition.Y);

        rect = new Rectangle(x, y, width, height);

        xx = x;
        yy = y;
        ww = width;
        hh = height;

        Refresh();
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {


        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {

            //pictureBox1.Cursor = Cursors.Default;
            draw = false;

            rectang = new UserRect(rect);
            rect = new Rectangle(e.X, e.Y, 0, 0);

            rectang.SetPictureBox(this.pictureBox1);   
        }
        else if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
        pictureBox1.Cursor = Cursors.Default;
        _tracking = false;

        }


    }

1 个答案:

答案 0 :(得分:0)

通常,我会根据需要在_Paint事件之间拆分图形(用于交互式图形,例如选择框和鼠标移动相关的东西)和单独的底图。

原因有很多,例如控制要绘制的内容(想象一下,如果未选中某些复选框,则仅绘制矩形1、3和5,然后绘制所有1至6(如果绘制) (已检查)和性能(包含大量数据)。 您可以在_Paint中完成所有这些操作,但是会膨胀。与此相反,您可以将绘图任务分为几个功能,这对于大型绘图非常方便。

非常简化示例-VB.NET:

Dim UnderlayingImage as Image
UnderLayingImage = New Bitmap(Me.PictureBox1.Width, Me.Picturebox1.Height)

Dim g As Graphics = Graphics.FromImage(UnderLayingImage)
Dim bgBrush As SolidBrush = New SolidBrush(Color.LightSlateGray)
Dim bgBrushWhite As SolidBrush = New SolidBrush(Color.White)
Dim shPen As Pen = New Pen(Color.Black)

Dim rect As RectangleF = New RectangleF(50, 50, 100, 100)
g.FillRectangle(bgBrush, rect)
g.DrawRectangle(Pens.Black, Rectangle.Round(rect))

Me.PictureBoxDrawing.Image = UnderLayingImage 

翻译成C#:

Image UnderlayingImage;
UnderLayingImage = new Bitmap(this.PictureBox1.Width, this.Picturebox1.Height);

Graphics g = Graphics.FromImage(UnderLayingImage);
SolidBrush bgBrush = new SolidBrush(Color.LightSlateGray);
SolidBrush bgBrushWhite = new SolidBrush(Color.White);
Pen shPen = new Pen(Color.Black);

RectangleF rect = new RectangleF(50, 50, 100, 100);
g.FillRectangle(bgBrush, rect);
g.DrawRectangle(Pens.Black, Rectangle.Round(rect));

this.PictureBoxDrawing.Image = UnderLayingImage;

回到您的问题:这样您就不会清除图形,而是重新绘制它,并在此过程中确定要包括的内容和要省略的内容。