jeaks
jeaks

Reputation: 77

c# draw function error

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

button resources

grey background with blue line

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

Answers (1)

Dirk Vollmar
Dirk Vollmar

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

Related Questions