首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DrawPath与DrawRectangle的区别

DrawPath与DrawRectangle的区别
EN

Stack Overflow用户
提问于 2017-05-12 11:09:38
回答 2查看 950关注 0票数 2

在我的应用程序中,我使用矩形和图形路径绘制线条,在使用graphicspath而不是使用矩形时,我在绘图中面临宽度和高度的损失。

下面是复制我的问题的示例代码,

代码语言:javascript
运行
复制
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绘图,因为我已经给出了适当的点,如矩形?

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-12 11:22:06

当您为您的Rectangle设置值时,您设置的是左上角的X和Y坐标以及宽度和高度。但是,使用GraphicsPath,您可以将每个角落显式地定义为一个单独的点。要使GraphicsPath绘制与Rectangle完全相同,您需要偏移点数组坐标以等于矩形的宽度和高度:

代码语言:javascript
运行
复制
Point[] points = new Point[]
{
   new Point(left, top),
   new Point(right + left, top),
   new Point(right + left, bottom + top),
   new Point(left, bottom + top),
};

或者构造矩形,将rightbottom视为坐标,而不是固定长度:

代码语言:javascript
运行
复制
Rectangle borderrectangle = new Rectangle(left, top, right - left, bottom - top);

考虑到您将这些值视为边值并因此进行协调,第二个选项可能会给出最一致的结果。

票数 1
EN

Stack Overflow用户

发布于 2017-05-12 11:14:45

当您创建矩形时,您将以宽度和高度的形式传递右侧和底部坐标。检查矩形构造函数参数:

代码语言:javascript
运行
复制
public Rectangle(
    int x,
    int y,
    int width,
    int height
)

当你使用路径时,你用坐标绘制它,一切都很好。您应该这样创建矩形:

代码语言:javascript
运行
复制
Rectangle borderrectangle = new Rectangle(left, top, right-left, bottom-top);

但请确保ClientRectangle的宽度和高度大于120

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43936364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档