c# - 从图像框中删除一个矩形
问题描述
我借用了一些代码在图像上绘制矩形,例如选择框。现在,只要您单击并拖动鼠标,代码就会绘制一个矩形。如果您只是左键单击而不拖动,则什么都不会发生 - 现有的矩形保持不变。如果单击并拖动一个新矩形,旧矩形就会消失。
这几乎和我想要的完全一样(我不想在图像上永久绘制......但是......),但有一个变化:我希望单击左键以使矩形也消失。
代码如下:
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()...
我怎样才能做到这一点?
编辑:我也尝试过保存图形状态并恢复它。那行不通...(没有错误,只是没有摆脱矩形)
解决方案
终于找到了一种方法,我将绘图保留在绘图事件中以提高性能/消除闪烁......
这一切都与 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());
}
}
}
}
这样做的好处是,如果我小心的话,我还可以使一些矩形永久化,同时删除其他矩形。
推荐阅读
- python - 保存选择时 Django 生产服务器错误 13 [已解决]
- php - jQuery PHP 使用向上和向下箭头键选择搜索结果
- javascript - CKEditor 4:在源视图和常规视图之间切换时 Javascript 消失
- reactjs - axios 没有获取 API
- mqtt - EMQX http ACL 认证 - 代理不可用
- node.js - 无服务器部署找不到无服务器域管理器
- spring-boot - Spring-boot 2.2.x 增加 CPU
- python - 每当我将一个模块导入我的代码时,整个模块都会运行
- python - 如何读取二进制数据并以二进制或十六进制格式打印?
- java - 无法将字符串转换为日期类型