【问题标题】:Removing a rectangle from imagebox从图像框中删除一个矩形
【发布时间】:2019-08-11 17:17:05
【问题描述】:

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

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

代码如下:

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()...

我怎样才能做到这一点?

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

【问题讨论】:

  • 一旦您在图形对象上泼了一些东西,就无法擦除它。你必须清除画布。
  • @Lars:他正在使用 Paint 事件,不是吗?解决方案:对 MouseUp 事件进行编码并测量 Rectangle 的大小。为空时不要重绘。在 MouseUp 中调用 invalidate 也是如此!
  • @TaW 我很喜欢你用这个去哪里......但不知道如何完成“不要重绘”这句话。
  • 等等...我想我现在关注...
  • Graphics.Save 保存的不是您绘制的内容,而是您对 Graphics 对象/工具所做的更改,例如缩放或旋转它。现在去睡觉了。你可能想看看这个post,它展示了我做非持久性绘图的方式。不确定您想要将应用程序的各个部分组合在一起..

标签: c# graphics


【解决方案1】:

终于找到了一种方法,我将绘图保留在绘制事件中以提高性能/消除闪烁...

这一切都与 fillRectangles

有关

这是工作代码:

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());
            }
        }

    }
}

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

【讨论】:

    猜你喜欢
    • 2017-07-02
    • 2015-12-16
    • 1970-01-01
    • 2014-04-22
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2018-10-04
    相关资源
    最近更新 更多