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
 
 
 
 
 
TitleDraw an image with gradient alpha (opacity) values in VB .NET
DescriptionThis example shows how to draw an image with gradient alpha (opacity) values in VB .NET.
Keywordsimage processing, alpha, gradient, opacity, VB .NET
CategoriesGraphics, VB.NET
 
When the program starts, it examines the pixels in two images and changes their alpha (opacity) components to form a gradient. The alpha values make the first image shade from transparent to opaque and they make the second image shade from opaque to transparent. The program draws the images onto an output Bitmap and displays the result. The final image shows an interesting blend of the two images.
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim bm_src1 As Bitmap = picSource1.Image.Clone
    Dim bm_src2 As Bitmap = picSource2.Image.Clone
    Dim bm_out As New Bitmap(bm_src1.Width, bm_src1.Height)
    Dim gr As Graphics = Graphics.FromImage(bm_out)

    ' Give the images alpha gradients.
    Dim alpha As Integer
    For x As Integer = 0 To bm_src1.Width - 1
        alpha = (255 * x) \ bm_src1.Width
        For y As Integer = 0 To bm_src1.Height - 1
            Dim clr As Color = bm_src1.GetPixel(x, y)
            clr = Color.FromArgb(alpha, clr.R, clr.G, clr.B)
            bm_src1.SetPixel(x, y, clr)

            clr = bm_src2.GetPixel(x, y)
            clr = Color.FromArgb(255 - alpha, clr.R, clr.G, _
                clr.B)
            bm_src2.SetPixel(x, y, clr)
        Next y
    Next x

    ' Draw the images onto the result.
    gr.DrawImage(bm_src1, 0, 0)
    gr.DrawImage(bm_src2, 0, 0)

    ' Display the result.
    picResult.Image = bm_out
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated