【问题标题】:how to clear the drawn rectangles in picturebox using graphics?如何使用图形清除图片框中绘制的矩形?
【发布时间】:2014-09-15 10:19:39
【问题描述】:

我在图片框中绘制了 12 个带有一些坐标的矩形,现在我想在将下一张图片加载到同一个图片框中之前清除图片框中绘制的矩形。 对于绘制矩形,我使用了以下代码,

>  g.DrawRectangle(pen1, rect);

其中 g 是图形,pen1= new System.Drawing.Pen(Color.Red, 2F); rect 是具有 x、y、宽度和高度坐标的矩形。

我还希望我绘制的图形可以调整大小,因为我使用了带有图片框 Mousedown、mousemove 和 Mouseleave 事件的 PosSizableRect Enum,并且我的矩形光标已更改,以便用户可以调整绘制的矩形坐标的大小。

如何在同一个 PictureBox 中加载下一个图像之前清除 PictureBox 中绘制的矩形? 我尝试了以下解决方案,但对我没有任何作用。 g.清除(颜色。红色); , this.Invalidate(); , 图片框1.Refresh();图片框1.图像=空;和 img.Dispose();

请指导我!!!我该如何继续?

【问题讨论】:

  • 这取决于你如何绘制矩形。如果您使用 g = pictureBox1.CreateGraphics(); 那么简单的 this.Invalidate(); 就可以了。如果您的图片框1 有图像,例如 pictureBox1.Image = img 并且您在 img 上绘图,那么您必须清除您的 img 并使之无效。显示一些代码

标签: c# winforms


【解决方案1】:

您是打算删除已绘制的矩形以将图像恢复到原始状态,还是打算在图片上绘制一个“清晰”(即白色)的矩形?

在第二种情况下,您应该使用 Graphics.FillRectangle。

在第一种情况下,您需要保留原始图像的副本并在要消除已绘制的矩形时重新绘制。

【讨论】:

    【解决方案2】:

    除非您实际存储使用的图像数据,否则这是无法实现的。除非你只想用 ex 填充图片框。白色的。我为您编写了一个简单的类来实现这一点。

    public class RestorablePictureBox : PictureBox
        {
            private Image _restoreImage;
            private Image _restoreBackgroundImage;
    
            protected override void OnPaint(PaintEventArgs pe)
            {
                if (_restoreImage != null) _restoreImage.Dispose();
                if (_restoreBackgroundImage != null) _restoreBackgroundImage.Dispose();
    
                _restoreImage = this.Image;
                _restoreBackgroundImage = this.BackgroundImage;
    
                base.OnPaint(pe);
            }
    
            public void Restore(bool fill = false)
            {
                if (fill)
                {
                    if (_restoreImage != null) _restoreImage.Dispose();
                    if (_restoreBackgroundImage != null) _restoreBackgroundImage.Dispose();
    
                    using (var gfx = this.CreateGraphics())
                    {
                        gfx.FillRectangle(Brushes.White, 0, 0, this.Width, this.Height); // Change Brushes.White to the color you want or use new SolidBrush(Color)
                    }
                }
                else
                {
                    if (_restoreImage != null) this.Image = _restoreImage;
                    if (_restoreBackgroundImage != null) this.BackgroundImage = _restoreBackgroundImage;
                }
            }
        }
    

    【讨论】:

    • 如何在我的代码中使用这个类,实际上我的代码中有一个清除按钮,当我点击那个按钮矩形应该从我的图片框中清除时,我在我的代码中复制了这个类。我是 C#.net 的初学者,请指导我,如何在我的代码中使用此类方法从图片框中清除矩形??
    • 将您的 PictureBox 更改为此 PictureBox。您可以从工具箱中拖动它(在构建一次之后)或像“var restorePic = new RestorablePictureBox();”一样创建它
    【解决方案3】:

    您可以通过以下代码使您的图片框为空白(完全擦除):

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

    【讨论】:

      猜你喜欢
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2018-10-23
      • 1970-01-01
      • 2014-12-19
      • 2013-09-16
      • 1970-01-01
      相关资源
      最近更新 更多