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 an image and changes their alpha (opacity) components to form a gradient. It then draws the image into a new Bitmap and displays the results. The final image shades from transparent on the left to opaque on the right.
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim bm_src As Bitmap = picSource.Image.Clone
    Dim bm_out As New Bitmap(bm_src.Width, bm_src.Height)
    Dim gr As Graphics = Graphics.FromImage(bm_out)

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

    ' Draw the image onto the result.
    gr.DrawImage(bm_src, 0, 0)

    ' Draw some text on the result.
    Dim layout_rect As New RectangleF(0, 0, bm_src.Width, _
        bm_src.Height)
    Dim string_format As New StringFormat
    string_format.LineAlignment = StringAlignment.Far
    string_format.Alignment = StringAlignment.Center
    Dim the_brush As New SolidBrush(Me.ForeColor)
    gr.DrawString("Smile!", Me.Font, the_brush, _
        layout_rect, string_format)
    the_brush.Dispose()

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