Hi James,
I've found this inconsisitency only with pens with width of 1. If you create
a pen with width more the 1 (2 let say) and set pen's alignment to
PenAlignment.Inset the rectangle will be exactly Rectangle.Width pixels wide
and Rectangle.Height pixels high.
In the book "Programming MS Windows with c#" Charles Petzold gives some
unclear explanation why when the pen's width is set to 1 the rectangle is 1
pixel higher and 1 pixel wider, which makes me think that MS finds this
reasonable. Anyways, I remember the way he explained to us the off-by-1
error in GDI in his book about Win32 and I feel that in his new book about
..NET he just tries the get off the subject as quick as it possible.
In GDI the drawing functions draw objects from the first point, up to the
last point but not including the last point. This decision was made to make
drawing connected lines with LineTo easier. If the last point was drawn this
would cause some points to be drawn twice, which in some ROPs could be
unallowable.
In GDI+ there are no ROPs so the lines can be drawn including the last
point. And it is as is. The same doesn't go for rectangles though. Let say
we want to draw a grid which lines are 1 pixel wide (I dare to say most of
the grids use lines 1 pixel wide ).
It could be done in several ways. And one of them is to use DrawRectangle or
DrawRectangles methods do draw the grid cells.
One posible drawing of grid 2x2 is:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
Pen pen = new Pen(Color.Red,1);
Rectangle[] r = new Rectangle[4]{new Rectangle(10, 10, 10, 10), new
Rectangle(20, 10, 10, 10), new Rectangle(10, 20, 10, 10), new Rectangle(20,
20, 10, 10)};
e.Graphics.DrawRectangles(pen, r);
}
The numbers used for the rectangles are pretty obvious and the the grid
looks exactly how it has to look. If it wasn't that off-by-1 the grid will
be surrounded with 1-pixel-wide line and the internal lines will be 2 pixels
wide or we would have used some others *not so obvious* numbers.
According to what MS did in Win32's GDI I believe this is the main reason
for this strange inconsisitency.
B\rgds
100
"James F. Bellinger" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> DrawRectangle draws from Left to Right, it appears.
> However, Rectangle seems to think the rectangle does
> not include the pixel referred to in its Right property..
> the range includes Left and every pixel up to but not
> including Right.
>
> You can see this strange inconsistency (though at which
> of the two it should be changed, I'm not sure ... I'd say
> DrawRectangle, and then Rectangle needs to have clearer
> documentation (largely because going:
>
> for (int i = r.Top; i < r.Bottom; i ++)
>
> Seems the way one would normally do things. You can see it
> by doing a DrawRectangle, constructing a Rectangle using
> the override that accepts a Size parameter. The right and bottom
> edges of the rectangle will be one pixel off. :-)
>
> Have a nice day 
> Jim Bellinger
>
>