【问题标题】:c# draw function errorc#绘制函数错误
【发布时间】:2016-11-29 20:25:00
【问题描述】:

所以我制作了这个函数,它使用图片框和图像作为参数绘制一个“按钮”。该按钮应该在活动时为蓝色,在不活动时为灰色,但不知何故,当按钮应该处于非活动状态时,该功能会在按钮上绘制一条蓝线

如何去掉那条蓝线?

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

【问题讨论】:

  • 这就是所有的代码吗?如果是,您的Properties.Resources.play_* 资源如何?
  • 您的目标是什么:Winforms、WPF、ASP..? 始终正确标记您的问题!
  • @DirkVollmar 我添加了一张图片。
  • @TaW 我正在使用 Winforms
  • Pbox 的 BackgroundColor 是蓝色的吗?不要使用MakeTransparent!您可能只使用默认颜色 (LightGray) 透明制作了一条线。使用 graph.Clear(Color.Transparent) 创建一个空位图!

标签: c# image winforms button draw


【解决方案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);

【讨论】:

  • 由于这些图片是Pngs,它们可能已经具有alpha透明度,根本不需要使用MakeTransparent
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
相关资源
最近更新 更多