I am working on a Windows Forms application in VS 2008, and I want to display one image over the top of another, with the top image being a gif or something with transparent parts.
Basically I have a big image and I want to put a little image on top if it, so that they kinda appear as one image to the user.
I've been trying to use a picturebox, but this doesn't seem to have worked, any suggestions?
Put the big/bottom image on a
PictureBox
, then add a handler to theOnPaint
event and use one of thee.Graphics.DrawImage()
overloads. You can load the image usingImage.FromFile()
.The small/top image will have to have an alpha channel and be transparent in the background for the overlay to work. You should be able to ensure this pretty easily in Photoshop or something similar. Make sure you save in a format that supports the alpha channel, such as PNG.
The vb.net code (All credits to Leon Tayson):
A list of similar posts is referenced at the bottom of this reply.
This reply addresses pictureBoxes and Winforms (in the other posts below, several reiterate that WPF solves this well already)
=
5 . In the 'paint' call for the 'background_pictureBox':
=
6 . Within the 'foreground_pictureBox_Paint' call, add in whatever graphics calls you want to be displayed in the foreground.
This topic repeats itself in several posts it seems:
how-to-make-picturebox-transparent
c-sharp-picturebox-transparent-background-doesnt-seem-to-work
make-overlapping-picturebox-transparent-in-c-net
a-picturebox-problem
PictureBox has 2 layers of images: BackgroundImage and Image, that you can use independently of each other including drawing and clearing.
I've always found that I've had to composite the images myself, using a single picturebox or control. Having two pictureboxes with transparent parts has never worked for me.
I was in a similar situation a couple of days ago. You can create a transparent control to host your image.