Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a picture background transparent using C#?

I have 3 pictureBoxs each one have transparent image, like this:

enter image description here

To make the picture 2 and picture 3 transparent for picture 1 , I wrote this code:

    pictureBox2.Parent = pictureBox1;
    pictureBox3.Parent = pictureBox1;

Now, my problem: How can I make picture 2 transparent for picture 3?

like image 932
Free User Avatar asked Dec 07 '25 00:12

Free User


1 Answers

There's a limit to how well this will work, you are past that limit when you start to nest images. You'll then see that a PictureBox is only transparent against its Parent, parts of the composite image where other PBs contribute pixels won't be visible. You'll see the Parent's background instead.

You'll need to switch to a single PictureBox and write code. Implement its Paint event handler and call e.Graphics.DrawImage() to draw the images. Layering is now no longer a problem, paint is always transparent against its background. Also the way that WPF implements transparency.

like image 80
Hans Passant Avatar answered Dec 08 '25 14:12

Hans Passant