Search code examples
c#transparency

C# transparency of an image on a form panel


I have been trying to show a circular image on a form and not show the corners of that image because I want to rotate the image at run time. I have tried setting the corners transparent in the paint program but it appears that is not supported. I tried the transparency key of the image but the result is the background is completely clear even though I have a form and a panel behind that I want to be seen. Note that this picture is on a panel that is on a form. Both the form and the panel have a background image that is wood grain. My code:

Image wheelImage = new Bitmap(@"C:\\Users\\Dave\\Documents\\Visual Studio 2012\\Projects\\Roulette_v1\\Roulette_v1\\Wheel_R173_G255_B91_V2.png");
Wheel_panel.BackgroundImage = wheelImage;
Wheel_panel.ForeColor = Color.FromArgb(173, 255, 91); //this matches the corners of the picture.
TransparencyKey = Color.FromArgb(173, 255, 91);

This is the image: enter image description here

This is the result: enter image description here

I also tried using the A value in FromArgb but it did nothing.


Solution

  • private void Form1_Load(object sender, EventArgs e)
            {
                // background of winform is Transparent;
                this.BackColor = Color.Red; this.TransparencyKey = Color.Red;
                // load image
                var wheelImage = new Bitmap("3K63tg0l.png");
                // change background to transparent
                var image_background_old = wheelImage.GetPixel(50, 50);
                for (int i = 0; i < wheelImage.Width; i++)
                {
                    for (int j = 0; j < wheelImage.Height; j++)
                    {
                        if (wheelImage.GetPixel(i,j) == image_background_old)
                        {
                            wheelImage.SetPixel(i, j, Color.Transparent);
                        }
                    }
                }
                // SET
                this.pictureBox1.Image = wheelImage;
            }