DrawPath和DrawRectangle之间的区别 [英] Difference between DrawPath and DrawRectangle
问题描述
大家好,
我在我的应用程序中使用Rectangle和graphicspath绘制线条,我在使用时面临绘图中宽度和高度的损失GraphicsPath而不是使用Rectangle。
$
下面是重现我的问题的示例代码,
Hi All,
I am drawing lines using both Rectangle and graphicspath in my application and i am facing lose of width and height in drawing when using GraphicsPath rather than using Rectangle.
Below is the sample code which reproduces my issue,
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);
}
这是图片,
Amal Raj U
Amal Raj U
推荐答案
Rectangle的构造函数确实期望with和height,而不是right和bottom。因此,DrawRectangle绘制一个更大的矩形,因为右下角位于(左+右,上+下)。虽然路径的右下角位于(右下角)。
The constructor of Rectangle does expect with and height, not right and bottom. So the DrawRectangle draws a larger rectangle, because bottom right corner is at (left+right, top+bottom). While the path has the bottom right corner at (right, bottom).
您可以通过创建矩形来解决此问题:new Rectangle(left,top,right - left,bottom - top) ;
You can fix this by creating rectangle as : new Rectangle(left, top, right - left, bottom - top);
这篇关于DrawPath和DrawRectangle之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!