我在我的应用程序中使用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绘图更新宽度和高度丢失的原因,因为我给出了像矩形一样的正确点?
任何帮助将不胜感激。
答案 0 :(得分:2)
创建矩形时,您将右侧和底部坐标作为宽度和高度传递。检查Rectangle构造函数参数:
const A*
当您使用Path时,您将按坐标绘制它,一切正常。你应该这样创建矩形:
public Rectangle(
int x,
int y,
int width,
int height
)
但请确保ClientRectangle的宽度和高度大于120