DrawPath 和 DrawRectangle 的区别
Difference between DrawPath and DrawRectangle
我在我的应用程序中同时使用 Rectangle 和 graphicspath 绘制线条,并且在使用 GraphicsPath 而不是使用 Rectangle 时,我在绘图中面临宽度和高度丢失的问题。
下面是重现我的问题的示例代码,
protected override void OnPaint(PaintEventArgs e)
{
int left = ClientRectangle.X + 40, right = ClientRectangle.Width -80;
int bottom = ClientRectangle.Height - 80, top = ClientRectangle.Y + 40;
int borderWidth = 10;
Rectangle borderrectangle = new Rectangle(left, top, right, bottom);
Pen pen = new Pen(Color.Black, borderWidth);
//Draws lines using Rectangle.
e.Graphics.DrawRectangle(pen, borderrectangle);
Point[] points = new Point[]
{
new Point(left, top),
new Point(right, top),
new Point(right, bottom),
new Point(left, bottom),
};
GraphicsPath path = new GraphicsPath();
path.AddLines(points);
path.CloseFigure();
//Draws lines using Path.
e.Graphics.DrawPath(pen, path);
}
这是图片,
内部矩形使用 DrawPath 绘制,外部矩形使用 DrawRectangle 绘制。
谁能告诉我 GraphicsPath 绘图时宽度和高度丢失的原因,因为我已经给出了正确的点,例如矩形?
我们将不胜感激。
当您创建矩形时,您传递的是右侧和底部 坐标 作为宽度和高度。检查 Rectangle 构造函数参数:
public Rectangle(
int x,
int y,
int width,
int height
)
你用Path的时候,是按坐标画的,一切正常。你应该这样创建矩形:
Rectangle borderrectangle = new Rectangle(left, top, right-left, bottom-top);
但要保证ClientRectangle的宽高都大于120
当您设置 Rectangle
的值时,您设置的是左上角的 X 和 Y 坐标以及宽度和高度。但是,对于 GraphicsPath
,您明确地将每个角定义为一个单独的点。要使 GraphicsPath
准确绘制 Rectangle
的内容,您需要将点数组坐标偏移为等于矩形的宽度和高度:
Point[] points = new Point[]
{
new Point(left, top),
new Point(right + left, top),
new Point(right + left, bottom + top),
new Point(left, bottom + top),
};
或构造矩形以将 right
和 bottom
视为坐标而不是固定长度:
Rectangle borderrectangle = new Rectangle(left, top, right - left, bottom - top);
考虑到您将值视为边并因此视为坐标,第二个选项可能会给您带来最大的一致性。
我在我的应用程序中同时使用 Rectangle 和 graphicspath 绘制线条,并且在使用 GraphicsPath 而不是使用 Rectangle 时,我在绘图中面临宽度和高度丢失的问题。
下面是重现我的问题的示例代码,
protected override void OnPaint(PaintEventArgs e)
{
int left = ClientRectangle.X + 40, right = ClientRectangle.Width -80;
int bottom = ClientRectangle.Height - 80, top = ClientRectangle.Y + 40;
int borderWidth = 10;
Rectangle borderrectangle = new Rectangle(left, top, right, bottom);
Pen pen = new Pen(Color.Black, borderWidth);
//Draws lines using Rectangle.
e.Graphics.DrawRectangle(pen, borderrectangle);
Point[] points = new Point[]
{
new Point(left, top),
new Point(right, top),
new Point(right, bottom),
new Point(left, bottom),
};
GraphicsPath path = new GraphicsPath();
path.AddLines(points);
path.CloseFigure();
//Draws lines using Path.
e.Graphics.DrawPath(pen, path);
}
这是图片,
内部矩形使用 DrawPath 绘制,外部矩形使用 DrawRectangle 绘制。
谁能告诉我 GraphicsPath 绘图时宽度和高度丢失的原因,因为我已经给出了正确的点,例如矩形?
我们将不胜感激。
当您创建矩形时,您传递的是右侧和底部 坐标 作为宽度和高度。检查 Rectangle 构造函数参数:
public Rectangle(
int x,
int y,
int width,
int height
)
你用Path的时候,是按坐标画的,一切正常。你应该这样创建矩形:
Rectangle borderrectangle = new Rectangle(left, top, right-left, bottom-top);
但要保证ClientRectangle的宽高都大于120
当您设置 Rectangle
的值时,您设置的是左上角的 X 和 Y 坐标以及宽度和高度。但是,对于 GraphicsPath
,您明确地将每个角定义为一个单独的点。要使 GraphicsPath
准确绘制 Rectangle
的内容,您需要将点数组坐标偏移为等于矩形的宽度和高度:
Point[] points = new Point[]
{
new Point(left, top),
new Point(right + left, top),
new Point(right + left, bottom + top),
new Point(left, bottom + top),
};
或构造矩形以将 right
和 bottom
视为坐标而不是固定长度:
Rectangle borderrectangle = new Rectangle(left, top, right - left, bottom - top);
考虑到您将值视为边并因此视为坐标,第二个选项可能会给您带来最大的一致性。