Re: What property defines a controls paint bounds?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Wed, 27 Feb 2008 13:20:02 -0800, Tom P. <padilla.henry@xxxxxxxxx> wrote:

I am creating a custom control and I'm trying to get the painting of
it correct. I'd like to simply use:
e.Graphics.FillRectangle(Brushes.White, DisplayRectangle);
... or ...
e.Graphics.FillRectangle(Brushes.White, ClientRectangle);
... or ...
e.Graphics.FillRectangle(Brushes.White, Bounds);

But each of those fails to draw a rectangle all the way around (most
are 1 pixel off). Instead I find myself having to use:
e.Graphics.FillRectangle(Brushes.White, DisplayRectangle.X,
DisplayRectangle.Y, DisplayRectangle.Width - 1,
DisplayRectangle.Height - 1);

What am I really supposed to use? How do I get that last pixel
correct?

Just what you're doing. Either DisplayRectangle or ClientRectangle are appropriate, depending on your specific need (by default, they are the same). And you need to subtract one from the width and height to get the whole rectangle to draw.

This may seem arbitrary, but there's actually a good reason for it. Graphical coordinates are treated as being the intersection of 0-width lines in a grid. The pixels are _between_ these lines. A line drawn from (0,0) to (0,100) for example will fill all of the pixels just to the right of that 0-width vertical line between those coordinates, inclusive.

When you try to draw a rectangle a specific width and height, all of the pixels are being drawn to the right and below of any coordinate that describes the rectangle. This means that on the right and bottom of the rectangle, if you've specified coordinates that are actually the absolute outer bounds of the area in which the control can draw, those pixels fall outside of the control and aren't drawn.

The fix is to reduce the width and height of the rectangle you're trying to draw by the width of one pixel, so that the pixels drawn for that rectangle fall within the control's drawable area.

Pete
.



Relevant Pages

  • RE: Update to Graphics shape quality problem
    ... rectangles/lines, the reported problem of missing pixels, is resolved. ... GDI+ is being asked to draw fractional pixels ... has caused shapes to not be exactly in position with respect to ... > In my application I draw a series of rectangle type shapes across the view ...
    (microsoft.public.dotnet.framework.drawing)
  • Is stencil what I want?
    ... all the pixels that weren't drawn into. ... want to draw the overlay first and count on the geometry to obscure it. ... In order to get the overlay to draw only on those pixels that haven't ...
    (comp.graphics.api.opengl)
  • Re: fast drawing in delphi
    ... I have to write the whole bunch of drawing functions ... >> but it doesn't show how to draw a simple rectangle using scanline... ... > By drawing all of its pixels, ...
    (comp.lang.pascal.delphi.misc)
  • Re: Rubber Band Box Over Tiff
    ... > the rectangle to be drawn with the mouse, ... but how do I redraw them with the actual image? ... Don't draw the box directly on the image, ...
    (comp.lang.java.programmer)
  • Re: What property defines a controls paint bounds?
    ... The pixels are _between_ these lines. ... A line drawn from ... When you try to draw a rectangle a specific width and height, ...
    (microsoft.public.dotnet.languages.csharp)