Smooth scaling of System.Drawing.Pen object?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi,

The code below draws with a Graphics object that is
scaled differently horizontally (50x) and vertically
(10x). If I draw a rectangle with a pen wider than 1
pixel then the top and bottom are 5x thicker than
the sides. To compensate for that I scale the Pen
in the "opposite" way. That successfully makes all
4 sides of the rectangle draw with the same
thickness.

What I don't understand is the "threshold" effect
I see when increasing the width of the scaled pen.
The code below shows that for pen widths of 1 to 14
the resultant sides are just 1 pixel thick. When
the width reaches 15, the sides jump to 15 pixels
thick. Why can't I get sides with widths between
1 and 15 pixels?

Thanks,
Alan Cobb

------- Code below from Paint handler --------

this.ClientSize = new Size( 1000, 1000 );

// Horizontal and vertical are scaled differently:
//
float fHorizScaling = this.ClientSize.Width / 50000.0F;
float fVertScaling = this.ClientSize.Height / 10000.0F;

e.Graphics.ScaleTransform(
fHorizScaling, // 0.02 (= 1000/50000)
fVertScaling // 0.10 (= 1000/10000)
);

Pen oPen = new Pen(
Color.Green,
1 // Initial width of pen.
);

// Scale pen to compensate for the scaling of the Graphics
// object above, so that aspect ratio of pen is square again:

oPen.ScaleTransform(
1 / fHorizScaling, // 50 (= 1 / 0.02)
1 / fVertScaling // 10 (= 1 / 0.10)
);

//oPen.Width = 5; // Draws sides of rectangle 1 pixel thick.
//oPen.Width = 14; // Draws sides of rectangle 1 pixel thick.
oPen.Width = 15; // Draws sides of rectangle 15 pixel thick.
//oPen.Width = 20; // Draws sides of rectangle 20 pixel thick.
//oPen.Width = 50; // Draws sides of rectangle 50 pixel thick.

e.Graphics.DrawRectangle(
oPen,
5000, 500, // Origin X, Origin Y
40000, 9000 // Width, Height
);

.



Relevant Pages

  • Re: Calculate the interior of a rectangle
    ... Apart from a slightly esoteric case which really doesn't have a bearing on this and which I won't therefore mention, a pixel is the smallest unit that can be addressed on the display. ... You also need to take into account which method you are using to draw your stuff, and in some cases you need to do that for all lines, even single pixel lines. ... For example, if you are using a single pixel line thickness and you draw a rectangle using the VB Line method with coordinates - you will not get exactly the same sized rectangle as you would get if you instead drew it using the API Rectangle method with exactly the same - coordinates. ... For example, if you draw a rectangle using such a pen then the rectangle coordinates you specify will effectively describe the outer edge of the lines of drawn rectangle, not the centre point, no matter how thick the line is. ...
    (microsoft.public.vb.general.discussion)
  • 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: Make rectangle with managed D3D
    ... You shouldn't need to lock the rectangle. ... Just grab the Graphics and draw, ... then release the Graphics object. ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: Elliptical
    ... An elliptical area can be surrounded by a rectangle. ... Then you can draw the ellips inside the rectangle by getting the ... programming in stead of getpixel(), setpixelfor every pixel you want to ...
    (microsoft.public.dotnet.languages.csharp)
  • Rectangle odd behavior
    ... I am having a hard time understanding the logic behind the Rectangle object. ... If you draw this rectangle on the screen you will end up with a rectangle ... like the one shown below (The character "X" represents a pixel used to draw ...
    (microsoft.public.dotnet.languages.csharp)