I am trying to use Graphics.DrawRectangle(Pen, x, y, w, h) to draw a rectangle on a WinForm. I draw the rectangle at position (1, 1) with a width and height of 3 in a WinForm's Paint() method. I have also used FillRectangle, to contrast the methods' behaviors. Here are my function calls:
g.DrawRectangle(Pens.Red, 1, 1, 3, 3);
g.FillRectangle(Brushes.Red, 1, 1, 3, 3);
FillRectangle draws as expected, with the rectangle origin at (1, 1) and a width and height of 3.
DrawRectangle, on the other hand, draws a rectangle at (0, 0), with a width and height of 4, and with the top left pixel missing.
This behavior is inconsistent and radically counterintuitive, and I assume that I am either doing something wrong, or there is something about the renderer that I do not understand. I have read the documentation, but found nothing that explains DrawRectangle's strange behavior. Any thoughts?
Edit: I had been using PixelOffsetMode.Half on my Graphics object. With the PixelOffsetMode left to its default the rectangle drew at the right position, although it was still 4x4 rather than 3x3.