2
votes

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);
}

Here is the image ,enter image description here

Inner rectangle is drawn using the DrawPath and outer rectangle is drawn with DrawRectangle.

Could anyone please update me the reason for width and height lose with GraphicsPath drawing, since i have given proper points as like the rectangle?

Any help will be appreciated.

2
with DrawPath you can draw many irregular shapes. - Lei Yang
@LeiYang - yes offcourse. But how to draw without lose in weight and height. - Amal
Look at the constructor of Rectangle. The parameters are: x, y, width, height - Dennis_E
In addition to comments/answer, try this Rectangle borderrectangle = new Rectangle(left, top, right-left, bottom-top); and they would be the same size - Pikoh

2 Answers

2
votes

When you create Rectangle you are passing right and bottom coordinates as width and height. Check Rectangle constructor parameters:

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

When you use Path, you are drawing it by coordinates and everything is OK. You should create rectangle this way:

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

But make sure that width and height of ClientRectangle are greater than 120

1
votes

When you are setting the values for your Rectangle, you are setting the X and Y coordinates of the top left corner as well as width and height. However, with your GraphicsPath, you are explicitly defining every corner as a separate point. To make the GraphicsPath draw exactly what the Rectangle is, you'll either need to offset your point array coordinates to equal the width and height of the 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),
};

or construct the Rectangle to treat the right and bottom as coordinates instead of fixed lengths:

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

Considering that you are treating the values as sides and therefore coordinates, the second option will probably give you the most consistency.