|
|
Title | Overlay one picture on another using MakeTransparent in VB .NET |
Keywords | overlay, transparent, PaintPicture, VB.NET |
Categories | Graphics, VB.NET |
|
|
The MakeTransparent method finds the pixels of a certain color in an image and sets their alpha components to 0, making those pixels transparent. (As a side effect, it also sets those pixels' other components to 0 making the pixels black. This doesn't seem to be documented so you probably shouldn't rely on this behavior remaining unchanged in future versions of VB .NET).
This program makes a Bitmap containing a label image. It calls the Bitmap's MakeTransparent method to make its red pixels transparent. It displays the resulting label image so you can see what it looks like. The pixels that were red are now transparent so the image's background color shows through.
The program then makes Bitmap and Graphics objects representing the main image. It uses the Graphics object's DrawImage method to copy the label image onto the main image and displays the result.
|
|
Private Sub btnBlend_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnBlend.Click
' Use MakeTransparent to make red pixels transparent.
Dim bm_label As New Bitmap(picLabel.Image)
bm_label.MakeTransparent(Color.Red)
' Display the result.
picLabel.Image = bm_label
' Copy the label onto the main picture.
Dim bm_dog As New Bitmap(picDog.Image)
Dim gr As Graphics = Graphics.FromImage(bm_dog)
gr.DrawImage(bm_label, _
(bm_dog.Width - bm_label.Width) \ 2, _
bm_dog.Height - bm_label.Height)
' Display the result.
picDog.Image = bm_dog
End Sub
|
|
|
|
|
|