c#绘制功能错误

时间:2016-07-26 09:09:16

标签: c# image winforms button draw

所以我做了这个函数,绘制了一个带有图片框和图像作为参数的“按钮”。该按钮在激活时应为蓝色,在非激活时应为灰色但在某种程度上功能在按钮上显示蓝线时应该处于非活动状态

button resources

grey background with 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();
        }
    }

我的功能调用:

    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);
    }

1 个答案:

答案 0 :(得分:1)

问题似乎与透明度有关。显然蓝线来自之前活跃的按钮发光版。以下是我的猜测:

您正在调用bitmap.MakeTransparent方法,“使此位图的默认透明色透明。如果系统未指定透明色,则LightGray为透明色。

您的非活动按钮位图可能会使用LightGray#FFD3D3D3)作为现在显示为蓝色的像素,因为它们是透明的。

修复方法是为需要透明的像素使用另一种颜色,然后调用bitmap.MakeTransparent并将该颜色传递给方法,例如:

// Get the color of a background pixel.
Color backColor = myBitmap.GetPixel(1, 1);

// Make backColor transparent for myBitmap.
myBitmap.MakeTransparent(backColor);