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
 
 
 
 
 
TitleApply image filters to a picture in VB .NET
Keywordsfilter, image processing, pixels
CategoriesGraphics
 
A filter is an operation you apply to the pixels in an image. Spatial filters use the properties of the pixels in a region to determine the values of output pixels. For example, a pixel's new value might be the average of its original value and the original values of the pixels around it. This filter produces a blurring of the image.

The following code shows how the program applies a filter to a picture. For each pixel in the image, the program loops over the filter array multiplying its values by the red, green, and blue components of the nearby pixels. It ensures that the resulting components are between 0 and 255, and then sets the pixel's new value.

Note that the program places the result in a new Bitmap not the original Bitmap. If it places the values in the original, it would affect the results of future pixels.

 
' Apply a filter to the image and 
' display the result.
Private Sub ApplyFilter(ByVal filter(,) As Single, Optional _
    ByVal offset_r As Integer = 0, Optional ByVal offset_g _
    As Integer = 0, Optional ByVal offset_b As Integer = 0)
    Dim bm As Bitmap = DirectCast(m_OriginalBitmap.Clone(), _
        Bitmap)

    ' Apply the filter.
    Dim y_rank As Integer = filter.GetUpperBound(0) \ 2
    Dim x_rank As Integer = filter.GetUpperBound(1) \ 2
    Dim r As Integer
    Dim g As Integer
    Dim b As Integer
    For py As Integer = y_rank To bm.Height - 1 - y_rank
        For px As Integer = x_rank To bm.Width - 1 - x_rank
            ' Calculate the value for pixel (px, py).
            r = offset_r
            g = offset_g
            b = offset_b

            ' Loop over the filter.
            For fy As Integer = 0 To filter.GetUpperBound(0)
                For fx As Integer = 0 To _
                    filter.GetUpperBound(1)
                    With m_OriginalBitmap.GetPixel(px + fx _
                        - x_rank, py + fy - y_rank)
                        r += .R * filter(fx, fy)
                        g += .G * filter(fx, fy)
                        b += .B * filter(fx, fy)
                    End With
                Next fx
            Next fy

            ' Set the pixel's value.
            If r < 0 Then
                r = 0
            ElseIf r > 255 Then
                r = 255
            End If
            If g < 0 Then
                g = 0
            ElseIf g > 255 Then
                g = 255
            End If
            If b < 0 Then
                b = 0
            ElseIf b > 255 Then
                b = 255
            End If
            bm.SetPixel(px, py, Color.FromArgb(255, r, g, _
                b))
        Next px
    Next py

    ' Display the result.
    picResult.Image = bm
End Sub
 
For more information on graphics programming in Visual Basic, including a chapter on filtering, see my book Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated