Custom color doesn't work in DrawString

  • Thread starter Thread starter Larry
  • Start date Start date
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?
 
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.
 
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)
 
Back
Top