c#绘制函数错误

c# draw function error

所以我做了这个函数,它以图片框和图像作为参数绘制 "button"。该按钮在活动时应该是蓝色的,在不活动时应该是灰色的,但是当按钮应该处于非活动状态时,功能会以某种方式在按钮上画一条蓝线

如何删除那条蓝线?

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

这个问题似乎与透明度有关。显然,蓝线来自之前激活的按钮版本。这是我的猜测:

您正在调用 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);