从图像盒中删除矩形

时间:2019-03-20 21:29:01

标签: c# graphics

我借用了一些代码在图像上绘制一个矩形,例如就像一个选择框。现在,每次单击并拖动鼠标时,代码都会绘制一个矩形。如果仅单击鼠标左键而不拖动,则什么也没有发生-现有的矩形保持不变。如果单击并拖动一个新矩形,则旧矩形将消失。

这几乎与我想要的完全一样(我不想永久在图像上绘制……但是...),但有一个变化:我想单击一下以使矩形消失也是

代码如下:

public partial class ScreenSelection : Form
{
  private Point RectStartPoint;
  private Rectangle Rect = new Rectangle();
  private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 72, 145, 220));

  public ScreenSelection(DataTable buttonData)
  {
    InitializeComponent();
  }

  private void Canvas_MouseDown(object sender, MouseEventArgs e)
  {
    if (e.Button == MouseButtons.Left)
    {
      RectStartPoint = e.Location;
      Invalidate();
    }
  }

  private void Canvas_MouseMove(object sender, MouseEventArgs e)
  {
    if (e.Button != MouseButtons.Left)
      return;
    Point tempEndPoint = e.Location;
    Rect.Location = new Point(
        Math.Min(RectStartPoint.X, tempEndPoint.X),
        Math.Min(RectStartPoint.Y, tempEndPoint.Y));
    Rect.Size = new Size(
        Math.Abs(RectStartPoint.X - tempEndPoint.X),
        Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
    Canvas.Invalidate();
  }

  private void Canvas_Paint(object sender, PaintEventArgs e)
  {
    // Draw the rectangle...
    if (Canvas.Image != null)
    {
      if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
      {
        e.Graphics.FillRectangle(selectionBrush, Rect);
      }
    }
  }
}

我还让用户加载一个位图作为画布的图像,所以一旦用户这样做,canvas.image将不等于null。

那么如何使该矩形在单击鼠标左键时消失?我已经在左键单击上执行了无效操作,这显然并没有消除它。

我尝试通过以下方式强制在左键单击上设置矩形大小:

        if (e.Button == MouseButtons.Left)
        {
            RectStartPoint = e.Location;
            Rect.Height = 0;
            Rect.Width = 0;
            Invalidate();
        }

并尝试了Rect.Size,Rect = Rectangle.Empty,Canvas.Refresh()...

我该怎么做?

编辑: 我也尝试过保存图形状态并恢复它。那是行不通的...(没有错误,只是没有摆脱矩形)

1 个答案:

答案 0 :(得分:0)

最后找到了一种方法,可以将绘图保留在paint事件中以提高性能/消除闪烁...

这与fillRectangle s

有关

这是工作代码:

public partial class ScreenSelection : Form
{
    private Point RectStartPoint;
    private Rectangle Rect = new Rectangle();
    private Brush selectionBrush = new SolidBrush(Color.FromArgb(128, 72, 145, 220));
    private List<Rectangle> Rects = new List<Rectangle>();
    private bool RectStart = false;

    public ScreenSelection(DataTable buttonData)
    {
        InitializeComponent();
    }

   private void Canvas_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            if (RectStartPoint == e.Location)
            {
                int i = Rects.Count;
                if (i > 0) { Rects.RemoveAt(i - 1); }
                Canvas.Refresh();
            }
        }
    }

    private void Canvas_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            RectStartPoint = e.Location;
            int i = Rects.Count;
            if (i >= 1) { Rects.RemoveAt(i - 1); }
            RectStart = false;
            Canvas.Refresh();
        }
    }

    private void Canvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left)
            return;

        Point tempEndPoint = e.Location;
        Rect.Location = new Point(
            Math.Min(RectStartPoint.X, tempEndPoint.X),
            Math.Min(RectStartPoint.Y, tempEndPoint.Y));
        Rect.Size = new Size(
            Math.Abs(RectStartPoint.X - tempEndPoint.X),
            Math.Abs(RectStartPoint.Y - tempEndPoint.Y));
        if (!RectStart)
        {
            Rects.Add(Rect);
            RectStart = true;
        }
        else
        {
            Rects[(Rects.Count - 1)] = Rect;
        }
        Canvas.Invalidate(); 
    }

    private void Canvas_Paint(object sender, PaintEventArgs e)
    {
        // Draw the rectangle...
        if (Canvas.Image != null)
        {
            if (Rects.Count > 0)
            {
                e.Graphics.FillRectangles(selectionBrush, Rects.ToArray());
            }
        }

    }
}

这样做的好处是,如果我小心的话,我还可以在删除其他矩形的同时使某些矩形永久化。