VB .NET allows you to specify an "alpha" value for a pixel. This value gives the pixel's transparency. A value of 0 indicates transparent while a value of 255 indicates opaque.
This program examines the pixels in an image containing dark green text on a red background. It sets alpha to 0 for the red pixels so they are completely transparent. It sets alpha to 128 for the other pixels, making them transparent.
When it has finished, the program makes a Bitmap containing the main picture and makes a Graphics object attached to it. It uses the Graphics object's DrawImage method to copy the bitmap with adjusted alpha values onto the main picture. Finally, it displays the modified bitmap in a PictureBox.
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Dim bm_name As New Bitmap(picName.Image)
Dim X As Integer
Dim Y As Integer
Dim clr As Integer
' Set alpha values in the name.
For X = 0 To bm_name.Width - 1
For Y = 0 To bm_name.Height - 1
' See if the pixel is red.
If bm_name.GetPixel(X, Y).R = 255 Then
' Red. Make it transparent.
bm_name.SetPixel(X, Y, Color.FromArgb(0, _
bm_name.GetPixel(X, Y).R, _
bm_name.GetPixel(X, Y).G, _
bm_name.GetPixel(X, Y).B))
Else
' Green. Make it translucent.
bm_name.SetPixel(X, Y, Color.FromArgb(128, _
bm_name.GetPixel(X, Y).R, _
bm_name.GetPixel(X, Y).G, _
bm_name.GetPixel(X, Y).B))
End If
Next Y
Next X
Dim bm_dog As New Bitmap(picDog.Image)
Dim gr_dog As Graphics = Graphics.FromImage(bm_dog)
gr_dog.DrawImage(bm_name, (bm_dog.Width - _
bm_name.Width) \ 2, bm_dog.Height - 2 * _
bm_name.Height)
picDog.Image = bm_dog
End Sub
|