nearly every time I use Graphics.DrawRectangle
or Graphics.FillRectangle
(the int
versions) I seem to miss the pixels on the right and bottom border of the rectangle I want to draw.
What is the exact definition of which pixels get filled by
Graphics.FillRectangle(brush,x,y,width,height)
and
Graphics.DrawRectangle(pen,x,y,width,height) // pen is one pixel width, i.e. width=0f
and is there a logical explanation for it? (so I can finally remember the behaviour :) ...)
EDIT:
Using Oleg Zhuk's marvelous Try GDI+ application, I was able to find a curious difference between FillRectangle
and DrawRectangle
:
FillRectangle(brush,0,0,1,1)
draws a single dot at (0,0) which is somehow understandable (it's a rectangle with width and height one after all).
DrawRectangle(pen,0,0,1,1)
on the other hand draws a small 2 by 2 pixel rectangle.
The same behaviour is shown for other width/height combinations, DrawRectangle
always extends one pixel more to the left and the bottom (which is a little annoying e.g. when using the ClientRectangle
property to draw a frame around a control)
My first question solved (how to they behave...) there still remains the second one: Why do they behave this way?
See Question&Answers more detail:os