Reputation: 441
I am working on a form application in VS 2008 and got stuck when overlapping two images. These images are transparent so they should be able to blend together but the result is show below:
I have seen similar posts to this one but seem to not be able to figure out why these two images can not blend together. Below is the code for how I am going about it:
InitializeComponent();
this.Width = 700;
this.Height = 768;
Bitmap BackgroundImg = new Bitmap(@"C:\Users\Classic.bmp");
Bitmap img = new Bitmap(@"C:\Users\Lime.bmp");
backImage.Image = BackgroundImg;
backImage.Width = 700;
backImage.Height = 768;
backImage.Location = new Point(0, 0);
overImage.Image = img;
overImage.Width = img.Width;
overImage.Height = img.Height;
overImage.Parent = backImage;
//overImage.BackColor = Color.Transparent;
overImage.Location = new Point(200, 200);
backImage and overImage are pictureBoxes
Upvotes: 0
Views: 186
Reputation: 942368
Your problem is with the overImage.Parent property. A PictureBox supports transparency against its parent. But the parent of overImage is the form, not backImage. So you see the form as the background, not the image. Note how using the form's BackgroundImage property instead of backImage fixes the problem.
This happened because PictureBox is not a ContainerControl. So when you dropped overImage on the form, it merely looks like it is a child control of backImage. It is not, the designer made the form the parent. You can tell from the Location property and the View + (Other Windows) + Document Outline window. That window very clearly shows the parent-child relationships. Note how trying to drag overImage to make it a child of backImage doesn't work either.
Changing the Parent property to backImage is possible, you have to do it in code.
Another simple workaround is to not use PictureBox controls at all but just draw the images in the form's Paint event with e.Graphics.DrawImage(). Simple layers of paint, otherwise the way WPF implements transparency. Takes two lines of code, makes your UI faster as well.
Upvotes: 2
Reputation: 44
I think your problem comes from the fact that Bitmap images do not support transparency.
Try with .png (for example) and use the SetColorKey method described in the following link : http://msdn.microsoft.com/en-us/library/e7755txx.aspx
Upvotes: 0