在我的应用程序中,我使用矩形和图形路径绘制线条,在使用graphicspath而不是使用矩形时,我在绘图中面临宽度和高度的损失。
下面是复制我的问题的示例代码,
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绘图,因为我已经给出了适当的点,如矩形?
任何帮助都将不胜感激。
发布于 2017-05-12 11:22:06
当您为您的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);考虑到您将这些值视为边值并因此进行协调,第二个选项可能会给出最一致的结果。
发布于 2017-05-12 11:14:45
当您创建矩形时,您将以宽度和高度的形式传递右侧和底部坐标。检查矩形构造函数参数:
public Rectangle(
int x,
int y,
int width,
int height
)当你使用路径时,你用坐标绘制它,一切都很好。您应该这样创建矩形:
Rectangle borderrectangle = new Rectangle(left, top, right-left, bottom-top);但请确保ClientRectangle的宽度和高度大于120
https://stackoverflow.com/questions/43936364
复制相似问题