Re: difference between drawrectangle and fillrectangle

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

From: Frank Hileman (frankhil_at_no.spamming.prodigesoftware.com)
Date: 10/04/04


Date: Mon, 4 Oct 2004 15:21:44 -0700

It is not really a "best fit". Here is how it works.

Assume you are running with PixelOffsetMode set to None and no
anti-aliasing. When you draw the rectangle edge, the pen you are using is
width 1. Consider the x dimension: the pen is centered over the coordinate
10 and extends to the coordinate 12. Since the center of the coordinate 10
is at pixel 10.5, and the center of coordinate 12 is at pixel 12.5, the pen
will exactly cover the pixel 10 and pixel 12. This means the pen draws a
rectangle 2 coordinate units wide, and 3 pixels wide, from 10.5 to 12.5 in
pixel coordinates, extending half a pixel (half width) on either side: 10 to
13 in pixel coordinates.

When drawing the fill, you get exactly 2 pixels wide, because the fill has a
0-width edge. It goes exactly from 10.5 to 12.5 in pixel coordinates, which
is truncated to 10 to 12.

If you draw with SmoothingMode set to AntiAlias, you will see the fill now
drawn the same width as the rectangle drawn with the pen. Only now, you will
see the edges of the fill at a lower alpha level: this is because each edge
of the fill actually lies on a mid-pixel boundary, and contributes only 50%
of the alpha level on those pixels.

Bottom line: pens always draw a figure that is increased in width and height
by the pen width. Fills do not, unless anti-aliased.

Regards,
Frank Hileman

check out VG.net: www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor

"Justin Rogers" <Justin@games4dotnet.com> wrote in message
news:uGvb5olqEHA.2900@TK2MSFTNGP12.phx.gbl...
> This isn't a bug. Both of the APIs do a best fit match and that means
> the fill is often a bit short in where it stops. You normally want to do
> a fill followed by a draw.
>
> Think of it this way... If you fill a box with water, does it take up the
> entire box, or just the *inside* of the box. Shapes have insides and
> edges and they are two separate things.
>
> "Frank Vanderlinden" <Frank@etc.be> wrote in message
> news:3fec01c4aa15$8f05e5a0$a601280a@phx.gbl...
> > Hi,
> >
> > Can some one give me a reason for the following :
> >
> > Test the following code:
> >
> > Graphics g = this.CreateGraphics();
> >
> > g.DrawRectangle(new SolidPen(Color.Red), 10,10,2,2);
> > g.FillRectangle(new SolidBrush(Color.Green), 10,10,2,2);
> >
> > Normally you should expect a solid green rectangle, but if
> > you look close you'l see a red line at the right and at
> > the bottom of the square.
> >
> > It appears that the DrawRectangle draws 3 pixels !!
> >
> > Is this a bug ??
> >
> > Kind regards
> > Frank Vanderlinden
>
>



Relevant Pages

  • Re: Rectangle odd behavior
    ... The rectangle draws the line 1 pixel wide and draws it to the top-left edge ... DrawRectangle which means that Pen.Alignment does not work for 1 pixel width ... pens. ... > If you draw this rectangle on the screen you will end up with a rectangle ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Anti-aliased drawing
    ... The problem will be modifying it to draw arc's, ... Dim ErrorAdj As Long, ErrorAcc As Long ... Dim ErrorAccTemp As Long, Weighting As Long ... ' Draw the initial pixel, ...
    (microsoft.public.vb.general.discussion)
  • Re: Highlighting a line/arc
    ... Given I could draw lines and curves with a 20-wide pen, ... object on top of the highlight, maybe you could draw two highlight ... I didn't know there is a maximum width for pens. ... The problem comes when the drawing pen itself is having the maximum ...
    (microsoft.public.vc.mfc)
  • Re: Basic Graphic Questions
    ... All the stuff I mentioned in my previous response will of course come in handy for you anyway, but that one specific point (the speed of VB PictureBox properties versus GDI pens etc) is the only part of your question that I failed to answer. ... In most cases you don't need to worry about the speed of setting line colours etc because you would generally set a colour once and then draw lots of lines with it before needing to set it to a different colour, so the overall effect of the time to set the drawing colour etc is very small when compared with the total time taken to draw lots of lines in that colour. ... ' use GDI LineTo here to draw a blue line ...
    (microsoft.public.vb.general.discussion)
  • Re: Draw pixel
    ... picture box, but instead draw right onto the form. ... simple example, I would want to draw a square, pixel by pixel to the ... Private Declare Function SetPixel Lib "gdi32" _ ...
    (comp.lang.basic.visual.misc)