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
 
 
 
 
 
TitleConvert an image to grayscale using VB .NET
KeywordsVB .NET, graphics, image, grayscale, image processing
CategoriesGraphics
 
The program creates a Bitmap object initialized from the picture. That sets the Bitmap's size and color support.

The code then loops through each pixel in the bitmap, averaging the red, green, and blue components and using the result to set the pixel's new value. When it has set each pixel's value, the program sets the picture box's Image property to the bitmap.

 
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    Dim bm As New Bitmap(picCanvas.Image)
    Dim X As Integer
    Dim Y As Integer
    Dim clr As Integer

    For X = 0 To bm.Width - 1
        For Y = 0 To bm.Height - 1
            clr = (CInt(bm.GetPixel(X, Y).R) + _
                   bm.GetPixel(X, Y).G + _
                   bm.GetPixel(X, Y).B) \ 3
            bm.SetPixel(X, Y, Color.FromArgb(clr, clr, clr))
        Next Y
    Next X

    picCanvas.Image = bm
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated