gpt4 book ai didi

c# - DrawPath 和 DrawRectangle 的区别

转载 作者:可可西里 更新时间:2023-11-01 10:03:21 25 4
gpt4 key购买 nike

我在我的应用程序中同时使用 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);
}

这是图片,enter image description here

内部矩形使用 DrawPath 绘制,外部矩形使用 DrawRectangle 绘制。

谁能告诉我 GraphicsPath 绘图时宽度和高度丢失的原因,因为我已经给出了像矩形一样的适当点?

我们将不胜感激。

最佳答案

当您创建 Rectangle 时,您传递的是右侧和底部坐标作为宽度和高度。检查Rectangle构造函数参数:

public Rectangle(
int x,
int y,
int width,
int height
)

当你使用 Path 时,你是通过坐标绘制它,一切都很好。你应该这样创建矩形:

Rectangle borderrectangle = new Rectangle(left, top, right-left, bottom-top);

但要保证ClientRectangle的宽高都大于120

关于c# - DrawPath 和 DrawRectangle 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43936364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com