Custom color doesn't work in DrawString

L

Larry

I'm sure I've missed the obvious, but when I define a custom color, my
DrawString call from my Paint event doesn't seem to draw anything at
all.

void btnNav_Paint(object sender, PaintEventArgs e)
{
SolidBrush brush1 = new SolidBrush(Color.Blue);
SolidBrush brush2 = new SolidBrush(Color.FromArgb(0x0000FF));
StringFormat format =
(StringFormat)StringFormat.GenericTypographic.Clone();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
Rectangle drawingRect = ClientRectangle;
drawingRect.Inflate(-15, 0);
e.Graphics.DrawString("Text", this.Font, brush1, drawingRect,
format);
//e.Graphics.DrawString("Text", this.Font, brush2, drawingRect,
format);
}

When I use brush1, the text draws correctly. When I use brush2,
nothing seems to draw at all. Any color I create with Color.FromArgb()
fails, but the standard colors all work.

This code is in an object derived from Label. I just chained in my
Paint event handler, so the base class Paint is still being called:

this.Paint += new PaintEventHandler(btnNav_Paint);

I can't figure out why a custom color behaves differently from a
standard color. Any ideas?
 
T

TAB

Hi

With SolidBrush(Color.FromArgb(0x0000FF));
you are defining a Color with zero alpha, which means it will be invisible.
This should be either Color.FromArgb(0x000000FF) (32-bit),
or Color.FromArgb(255, 0x0000FF), alpha + color,
or Color.FromArgb(0, 0, 255) for blue, alpha assumed 255.
 
G

Göran Andersson

TAB said:
Hi

With SolidBrush(Color.FromArgb(0x0000FF));
you are defining a Color with zero alpha, which means it will be invisible.
This should be either Color.FromArgb(0x000000FF) (32-bit),

That still has an alpha component of zero.
or Color.FromArgb(255, 0x0000FF), alpha + color,

There is no such overload. You have to use a Color value:

Color.FromArgb(255, Color.FromArgb(0x0000FF))
or Color.FromArgb(0, 0, 255) for blue, alpha assumed 255.

That works. You can also specify the alpha component:

Color.FromArgb(255, 0, 0, 255)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top