By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

GDI+ question-rounded label border does not draw correctly

P: n/a
I'm trying to create a form with some rounded controls, and I'm having
some problems with the rounding not working correctly. I've tried a
couple of different methods for creating the graphics path, including
the one from the GDI+ FAQ. It's a pretty simple path, and
mathematically it looks just fine. When I apply it to a control
though, I have two problems. First, the corners on the right side of
the control are obviously not rounded as much as the ones on the left
side, and if I attempt to add an outline to the control (by drawing the
path), I get missing pixels that break my lines. Essentialy, here's
what I'm doing in my derived label class (I included the gradient brush
code in case that might be affecting things):

Rectangle rect = new Rectangle(0, 0, Width, Height);
LinearGradientBrush GBrush = new LinearGradientBrush(
new Point(0, 0),
new Point((int)(Width*1.2), 0), cLeft, cRight);
this.region=CreateRoundedRectPath(rect, 6);
e.Graphics.FillRectangle(GBrush, rect);
Pen p = new Pen(Color.Gray, 2);
e.Graphics.DrawPath(p, path);
SolidBrush drawBrush = new SolidBrush(this.ForeColor);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
RectangleF rectF = new RectangleF(0, 0, this.Width, this.Height);
e.Graphics.DrawString(this.Text, this.Font, drawBrush, rect, sf);

I'm using this code from the FAQ to calculate the path:

gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);

gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius *
2, 270, 90);

gp.AddLine(X + width, Y + radius, X + width, Y + height -
(radius * 2));

gp.AddArc(X + width - (radius * 2), Y + height - (radius *
2), radius * 2, radius * 2, 0, 90);

gp.AddLine(X + width - (radius * 2), Y + height, X +
radius, Y + height);

gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius
* 2, 90, 90);

gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);

gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);

return gp;

Anyone have any idea what's going on? Is this just a limitation of
GDI+ that you're never going to get things correct to the pixel? I
have to doubt it, simply because the labels are very obviously lopsided
with broken outlines.

Thanks in advance!


Sep 25 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.