[英]I want to draw Square on picturebox C#
I want to draw Square on Image not the rectangle when I perform mouse move operation to Top, Left, Bottom, Right direction its Height and Width will be increase in same length. 我想在Image上绘制Square而不是矩形,当我执行鼠标移动操作到Top,Left,Bottom,Right方向时,它的高度和宽度将以相同的长度增加。
below is my code it does not display square I want exact code for Square 下面是我的代码它不显示正方形我想要Square的确切代码
public Form1()
{
InitializeComponent();
}
Rectangle currRect;
Point endPoint;
bool isDrag;
Point startPoint;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
startPoint = new Point(e.X, e.Y); //
if (e.Button == MouseButtons.Left)
{
currRect = new Rectangle();
currRect.X = startPoint.X;
currRect.Y = startPoint.Y;
isDrag = true;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDrag)
{
endPoint = new Point(e.X, e.Y);
currRect.Width = endPoint.X - startPoint.X;
currRect.Height = endPoint.Y - startPoint.Y;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDrag = false;
Graphics graphics = this.pictureBox1.CreateGraphics();
graphics.DrawRectangle(new Pen(Brushes.Red), currRect.X, currRect.Y, currRect.Width, currRect.Height);
}
also one thing when I going to increase the size of square its starting point or we can say that its Top,Left co-ord remains steady or constant when I am increasing in Top, Left, Bottom, Right direction. 还有一件事,当我要增加它的起点的平方大小时,或者我可以说当我在上,左,下,右方向上增加时,它的上,左共同保持稳定或恒定。
this starting point is not steady when I move the mouse cursor its starting point gets change i don't want this so please help me to figure out from this situation 当我移动鼠标光标时,这个起点不稳定,它的起点变了,我不想这样,请帮我弄清楚这种情况
Your code is drawing a rectangle, not a square. 您的代码绘制的是矩形,而不是正方形。 If you want to draw a square, you need to make the width and height identical. 如果要绘制正方形,则需要使宽度和高度相同。
If you modify your pictureBox1_MouseMove
method to this, you can draw a square with the length equal to the maximum of the width and height: 如果将pictureBox1_MouseMove
方法修改为此方法,则可以绘制一个长度等于宽度和高度最大值的正方形:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDrag)
{
endPoint = new Point(e.X, e.Y);
int maxLength = Math.Max(endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
currRect.Width = maxLength;
currRect.Height = maxLength;
}
}
EDIT: Here is the solution that you can use to draw your square from any direction: 编辑:这是您可以用来从任何方向绘制正方形的解决方案:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDrag)
{
endPoint = new Point(e.X, e.Y);
currRect.X = Math.Min(startPoint.X, endPoint.X);
currRect.Y = Math.Min(startPoint.Y, endPoint.Y);
int maxLength = Math.Max(Math.Abs(startPoint.X - endPoint.X), Math.Abs(startPoint.Y - endPoint.Y));
currRect.Width = maxLength;
currRect.Height = maxLength;
}
}
Replace 更换
graphics.DrawRectangle(new Pen(Brushes.Red), currRect.X, currRect.Y, currRect.Width, currRect.Height);
By: 通过:
graphics.DrawRectangle(new Pen(Brushes.Red), currRect.X, currRect.Y, Math.Min(currRect.Width, currRect.Height), Math.Min(currRect.Width, currRect.Height));
Secondly, I don't see how the starting point changes? 其次,我看不出起点如何变化?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.