Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleOverlay one picture on another in Visual Basic 2008
DescriptionThis example shows how to overlay one picture on another in Visual Basic 2008.
Keywordsoverlay, transparent, image processing, picture, VB.NET
CategoriesGraphics, VB.NET
 
The program overlays one picture over another. The places where the foreground image should be transparent are drawn in red.

The program makes a new Bitmap and copies the foreground image into it. It then calls the Bitmap's MakeTransparent method to make the Bitmap's red pixels transparent.

Next the program creates a Bitmap to hold the result. It copies the background image into it, copies the foreground image on top, and then displays the result.

 
' Overlay the non-red parts of the foreground image on top
' of the background image.
Private Sub btnOverlay_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnOverlay.Click
    ' Copy the foreground image into a bitmap.
    Dim fg_wid As Integer = picForeground.Image.Width
    Dim fg_hgt = picForeground.Image.Height
    Dim fg_bm As New Bitmap(fg_wid, fg_hgt)
    Using gr As Graphics = Graphics.FromImage(fg_bm)
        gr.DrawImage(picForeground.Image, 0, 0, fg_wid, _
            fg_hgt)
    End Using

    ' Make the red pixels transparent.
    fg_bm.MakeTransparent(Color.Red)

    ' Make the result bitmap.
    Dim bg_wid As Integer = picBackground.Image.Width
    Dim bg_hgt = picBackground.Image.Height
    Dim bg_bm As New Bitmap(bg_wid, bg_hgt)
    Dim cx As Integer = (bg_wid - fg_wid) \ 2
    Dim cy As Integer = (bg_hgt - fg_hgt) \ 2
    Using gr As Graphics = Graphics.FromImage(bg_bm)
        gr.DrawImage(picBackground.Image, 0, 0, bg_wid, _
            bg_hgt)

        ' Draw the foreground image on top.
        gr.DrawImage(fg_bm, cx, cy, fg_wid, fg_hgt)
    End Using

    ' Display the result.
    picResult.Image = bg_bm

    ' We don't need the foreground bitmap any more.
    fg_bm.Dispose()
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated