Reputation: 77
So I made this function that draws a "button" with a picturebox and image as parameters. The button is supposed to be blue when active and grey when inactive but somehow the fuction draws a blue line over the button when it is supposed to be inactive
How do I remove that blue line?
void DrawButton(PictureBox PIC, Image IMG)
{
using (Font myFont = new Font("Century Gothic", 11))
{
Bitmap bitmap = new Bitmap(IMG, PIC.Width, PIC.Height);
bitmap.MakeTransparent();
Graphics graph = Graphics.FromImage(bitmap);
// PIC.Tag is "Next"
graph.DrawString(PIC.Tag.ToString(), myFont, Brushes.White, new Point(42, 0));
PIC.Image = bitmap;
graph.Dispose();
}
}
And the calls of my function:
private void picNext_MouseLeave(object sender, EventArgs e)
{
if (_CURRENT_PAGE * 12 < DB.GetOnlinePlayers())
DrawButton(picNext, Properties.Resources.play_no_hover);
else DrawButton(picNext, Properties.Resources.play_no_active);
}
private void picNext_MouseUp(object sender, MouseEventArgs e)
{
if (_CURRENT_PAGE * 12 < DB.GetOnlinePlayers())
DrawButton(picNext, Properties.Resources.play_hover);
else DrawButton(picNext, Properties.Resources.play_no_active);
}
private void picNext_MouseDown(object sender, MouseEventArgs e)
{
if (_CURRENT_PAGE * 12 < DB.GetOnlinePlayers())
DrawButton(picNext, Properties.Resources.play_take);
else DrawButton(picNext, Properties.Resources.play_no_active);
}
private void picNext_MouseEnter(object sender, EventArgs e)
{
if (_CURRENT_PAGE * 12 < DB.GetOnlinePlayers())
DrawButton(picNext, Properties.Resources.play_hover);
else DrawButton(picNext, Properties.Resources.play_no_active);
}
Upvotes: 1
Views: 63
Reputation: 176229
The issue seems related to the transparency. Apparently the blue line comes from the previously active version of the button shining through. Here is my guess what is going on:
You are calling the bitmap.MakeTransparent
method, which “makes the default transparent color transparent for this Bitmap. If no transparent color is specified by the system, LightGray
is the transparent color.”
Your inactive button bitmap probably uses LightGray
(#FFD3D3D3
) for the pixels that now appear blue because they are made transparent.
The fix is to use another color for the pixels that need to be transparent and then call bitmap.MakeTransparent
and pass that color into the method, e.g.:
// Get the color of a background pixel.
Color backColor = myBitmap.GetPixel(1, 1);
// Make backColor transparent for myBitmap.
myBitmap.MakeTransparent(backColor);
Upvotes: 1