Drawing a transparent button

后端 未结 4 1500
不知归路
不知归路 2020-11-29 07:35

I\'m trying to create a transparent button in C# (.NET 3.5 SP1) to use in my WinForms application. I\'ve tried everything to get the button to be transparent (it should show

4条回答
  •  独厮守ぢ
    2020-11-29 08:05

    I know this question is old, but if someone doesn't want to create a control to do this I came up with this code from a different article and changed it an extension method.

    public static void ToTransparent(this System.Windows.Forms.Button Button,
         System.Drawing.Color TransparentColor)
    {
        Bitmap bmp = ((Bitmap)Button.Image);
        bmp.MakeTransparent(TransparentColor);
        int x = (Button.Width - bmp.Width) / 2;
        int y = (Button.Height - bmp.Height) / 2;
        Graphics gr = Button.CreateGraphics();
        gr.DrawImage(bmp, x, y);
    }
    

    And the call like:

    buttonUpdate.ToTransparent(Color.Magenta);
    

提交回复
热议问题