|
|
Title | Overlay one image on another with alpha blending in VB .NET |
Keywords | alpha, alpha blending, transparent, translucent, semi-transparent |
Categories | Graphics, VB.NET |
|
|
Examine the pixels in the overlay image, setting their alpha values appropriately. Then use the Graphics object's DrawImage method to copy the overlay image onto the main image.
|
|
Private Sub btnBlend_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnBlend.Click
' Examine the pixels in the label. Make red pixels
' grow more transparent to the right and green
' pixels grow less transparent to the right.
Dim bm_label As New Bitmap(picLabel.Image)
Dim alpha As Integer
Dim clr As Color
Dim x As Integer
Dim y As Integer
For y = 0 To bm_label.Height - 1
For x = 0 To bm_label.Width - 1
clr = bm_label.GetPixel(x, y)
If clr.G = 0 Then
' A red pixel.
alpha = 255 - CInt(255 * x / bm_label.Width)
Else
' A green pixel. Set alpha = 127.
alpha = CInt(255 * x / bm_label.Width)
End If
bm_label.SetPixel(x, y, _
Color.FromArgb(alpha, clr.R, clr.G, clr.B))
Next x
Next y
' 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
|
|
|
|
|
|