我只需要用mousemove在图片框内绘制一个矩形。 不超过图片框的边界。绘制各个方向的矩形
向右或向下拖动对我来说工作正常...如何使moviment反向?
我的代码如下。
Rectangle Rect = new Rectangle();
private Point RectStartPoint;
public Pen cropPen = new Pen(Color.Red, 2);
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
RectStartPoint = e.Location;
picImagem.Invalidate();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Point tempEndPoint = e.Location;
Rect.Location = new Point(Math.Min(RectStartPoint.X, tempEndPoint.X),
Math.Min(RectStartPoint.Y, tempEndPoint.Y));
Rect = new Rectangle(
Math.Min(tempEndPoint.X, Rect.Left),
Math.Min(tempEndPoint.Y, Rect.Top),
Math.Min(e.X - RectStartPoint.X, picImagem.ClientRectangle.Width - RectStartPoint.X),
Math.Min(e.Y - RectStartPoint.Y, picImagem.ClientRectangle.Height - RectStartPoint.Y));
picImagem.Refresh();
picImagem.CreateGraphics().DrawRectangle(cropPen, Rect);
}
}
您的确切问题,如下面所回答的,与指定问题完全相同。你的代码中还有其他错误,例如未能捕获鼠标并使用'CreateGraphics()'来绘制到控件中,而不是处理'Paint'事件或绘制到位图中。重复的错误确实显示了绘制的正确方法。如果您对其他错误有疑问,请提出专门针对这些问题的新问题。 –
另请参阅[此处]的答案(https://stackoverflow.com/a/2529623/3538012)和[此处](https://stackoverflow.com/a/6087367/3538012)以获取更多灵感。 –