Very simple code here...just drawing what should be a 64x64 rectangle in the OnPaint
routine for a Form
:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(32, 32, 64, 64));
}
I took the following screen capture, and measured the square, which was 65x65.
Just wondering what causes the extra 1px width/height, and how to fix?
EDIT...
I know I can fix using Rectangle.Inflate(-1, -1)
...more to the point, I want to know why GDI/GDI+ renders the rectangle 1px wider/higher than specified, and in this context, give reason for using the Inflate
fix.
The centre of each border edge is drawn to the left of or below the actual edge of the rectangle. For a single-pixel border, the top edge and bottom edge are drawn below the actual edge of the rectangle. If you draw such a rectangle at the top of the form then the Top of that rectangle is 0 and the top edge is drawn in the space between 0 pixels and 1 pixel. If that rectangle is 64 pixels high then the Bottom is 64 and the bottom edge is drawn between 64 pixels and 65 pixels. If you set the thickness of the pen to 2 then you'll find the the other half of the bottom edge is drawn between 63 and 64 pixels. The vertical edges are likewise, i.e. drawn to the right for the first odd pixels and the left for even.