Groups | Blog | Home
all groups > dotnet drawing api > january 2005 >

dotnet drawing api : GDI+, Pens and Draw*** methods


Andreas Håkansson
1/31/2005 12:36:22 AM
From what I have been able to gather I have understood that because of the
nature which GDI+ works, the Draw** methods will always draw 1 more pixel
than you had expected.

One explination I read for this is because it accounts for the width of the
pen, just as it would in real life drawing.

For those out there who doesn't know what I'm talking about, the following
will result in a 101x101 rectangle (messuring from border to border)

Rectangle r = new Rectangle(0,0,100,100);
e.Graphics.DrawRectangle(Pens.Black, r);

So in effect I would need to use a rect with a width and height of 99 to get
the desired results. This means that if I have as shape which I define by
Left, Top, Width and Hight properties I would have to create my rect like so

Rectangle r = new Rectangle(this.Left, this.Top, this.Width - 1,
this.Height - 1);

Which is pretty annoying (atleast for me) and the same goes if I were to
create the rectangle by drawing four lines, I would still have to substract
1 pixel.

I was pointer towards the PenAlignment property of the Pen object, but it
seems that it's only Outset and Inset which has any effect (?).

So this leavs me with three options

(a) Live with this fact and always subtract 1 pixel

(b) Create my own DrawRect method which wraps the "subtract 1 pixel
logic", thus saving me from doing over and over again in different places.

(c) Hope that someone whom reads this has a few wize words to share about
this which hopefully will give me the desired results without having to
resort to (a) and (b)

Looking forward to hearing from you =)

//Andreas

Bob Powell [MVP]
1/31/2005 11:52:55 AM
You just have to remember that 0 counts as a number also so 0-100 is 101
pixels.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

Andreas Håkansson
1/31/2005 1:53:09 PM
Bob,

True but when I say that something has a Width of x-pixels, then it should
be x-pixels wide not x+1. I would accept this argument if I were to define
Right and Bottom properties so which would give me

Left = 0
Right = 100

Equals a 101 pixel wide box since 0 also counts as a pixel. A width of 100
is always a width of 100 despite where it's drawn. Don't you agree?

So the question remains, is there any work-around for this which doesnt
require me to subtract 1 each time I draw ?

..Andreas

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> skrev i meddelandet
news:uKJduL4BFHA.3088@TK2MSFTNGP10.phx.gbl...
[quoted text, click to view]

Bob Powell [MVP]
1/31/2005 2:39:55 PM
Ah I see what you're getting at.

After looking into this a little deeper I find that the following code:

e.Graphics.FillRectangle(Brushes.Black,200,1,5,5);
provides a rectangle of 5*5 pixels and

e.Graphics.FillRectangle(Brushes.Black,200,1,20,20);

provides a rectangle of 20*20

The DrawRectangle however, is designed to follow the unfortunate precedent
set out by GDI's rectangle method which draws the rectangle one pixel larger
on each side than was requested. This was a known design consideration in
the early days of GDI+ and it was done this way to maintain a level of
compatibility for vendors who were converting graphics code wholesale to
GDI+. Whether the choice was right or wrong is subjective.

To get a rectangle 100 wide you need to specify 99... There is no "work
around" for this intended behaviour.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

Mick Doherty
1/31/2005 3:03:20 PM
I have to agree with Andreas, the method is wrong.

Whilst I would accept your explanation for this behaviour, it should be a
consistent behaviour.

Change the width of the pen to >1 and set it's alignment to Inset and it
will draw inside the rectangle. This is the behaviour I expect to see when I
have explicitly set the PenAlignment property, regardless of the width of
the Pen.

Why is there different behaviour for Pens of different Widths?

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


[quoted text, click to view]

Andreas Håkansson
1/31/2005 11:20:22 PM
Oh my, this was a suprise. I tried it out and once I got above a pen width
of
1.5 it started to draw dirrectly if the PenAlignment had been set to Inset.

This is a very disturbing side effect. Iäm sure it can be traced back to the
unit-independant nature of GDI+ and the fact that you can't really draw
fractional pixels without approximations - still sucks though.

/Andreas

"Mick Doherty"
<EXCHANGE#WITH@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:uMIfCY6BFHA.1392@tk2msftngp13.phx.gbl...
[quoted text, click to view]

AddThis Social Bookmark Button