Click here to Skip to main content
Licence Ms-PL
First Posted 10 Jan 2012
Views 9,766
Downloads 0
Bookmarked 23 times

VB.NET Image Filters

By | 4 Feb 2012 | Article
A collection of 10 easy to use image filters in VB.NET
filters_small.png - Click to enlarge image

Introduction

I have seen a few image filters written in VB, but most are poorly implemented, slow or just overly-complex. So I decided to create a simple class that contains a variety of filters and is easy to use.

Background

I originally created this class to render a drop shadow on a custom shaped tooltip that I created in my spell check. Somewhat bored over my break, I decided to expand on this class and it quickly grew into its own project.

Using the Code

Using the filters is as easy as going:

Using b As New Bitmap("c:\test.png")
    b.Filters.DropShadow(Color.Black, New Point(8, 8), 4)
    b.Save("c:\test.png", Imaging.ImageFormat.Png)
End Using

The above will open the file c:\test.png, apply the DropShadow filter, and re-saves the image over the original file.

More details on specific filters and their options are listed later in this section.

The code can only be used on a bitmap object, so if you want to draw directly in a paint event, you will need to first render to a bitmap object like so:

Private Sub Form_Paint(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    'Create a bitmap object to draw onto
    Using b As New Bitmap(128, 128)
        'Create a graphics object so we can render onto the bitmap
        Using g = Graphics.FromImage(b)
            'Make graphics edges nice and "smooth"
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            'Draw a circle
            g.DrawEllipse(Pens.Red, New Rectangle(10, 10, 108, 108))
        End Using
        'Static the circle that we drew with amount of 8
        b.Filters.Static(8)
        'Draw the output to the form
        e.Graphics.DrawImage(b, New Point(0, 0))
    End Using
End Sub

The result of the code above will paint the following on your form:

In total there are 10 filters, which can be seen in the test projects. The filters and their settings are as follows:

'Alpha
'Makes the image translucent by the value specified
b.Filters.Alpha(Amount As Byte)
'Amount (0 - 255)
'    The amount of alpha to apply to the bitmap 0=Transparent 255=Opaque

'AlphaMask
'Creates a silhouette based on the alpha channel of an image
b.Filters.AlphaMask(AlphaColor As Color, SolidColor As Color)
'AlphaColor
'   The color that the transparent sections will be in the resulting bitmap
'SolidColor
'   The color that the solid sections will be in the resulting bitmap

'Brightness
'Makes the bitmap lighter or darker
b.Filters.Brightness(amount As Single)
'amount (-1 - 1)
'    The amount of brightness to apply to the bitmap 
'    (negative numbers darken the image positive lighten)

'DropShadow
'Adds a drop shadow to an image with alpha
b.Filters.DropShadow(ShadowColor As Color, Depth As Point, BlurAmount As Integer)
'ShadowColor
'    The color of the resulting shadow
'Depth
'    The x and y offsets for the shadow depth
'BlurAmount (2 +)
'    How blurred the shadow will appear

'Emboss
'Adds an emboss effect to the bitmap
b.Filters.Emboss()

'GausianBlur
'Applies a blur to the bitmap
b.Filters.GausianBlur(Amount As Integer)
'Amount (2 +)
'    Specifies the amount that the image will be blurred in pixels

'GrayScale
'Converts the bitmap a GrayScale Image
b.Filters.GrayScale()

'HSL
'Adjusts the Hue, Saturation and Luminance of a bitmap
b.Filters.HSL(hue As Single, sat As Single , lum As Single)
'hue (0 - 359 but can be any number .. if 361 or 721 is used this is
'equivalent to 1, 362 or 722 = 2 etc, 
'NOTE: some people prefer to use -180 to + 180 instead)
'    defines the hue of the resulting bitmap (0 = No Change)
'sat (-1 - 1)
'    defines the saturation of the resulting bitmap (0 = No Change)
'lum (-1 - 1)
'    defines the Luminance of the resulting bitmap (0 = No Change)

'Invert
'Inverts the colors of the bitmap object
b.Filters.Invert

'Static
'Mixes the pixels up of the image to produce a static like effect 
'(Although no actual static is added :))
b.Filters.Static(Amount As Integer)
'Amount (0 +)
'    The zone (in pixels) to swap with other pixels (0 = No Change)

The Test Project

The test project includes two testing forms. The form that opens when you run the project (pictured below) allows you to load an image file and then specify the filters (and appropriate options for each filter) that you wish to apply.

There is also a simple test that just shows all of the filters that are possible with preset options (pictured below); this can be accessed by clicking the "Simple Test" button on the main form.

filters_small.png - Click to enlarge image

Downloads

Total downloads:

Downloads per day:

Change Log

20120203

  • Added OilPainting Filter

20120103

  • Initial Release

Credits

All of the filters have been written by Kris Bennett here at i00 Productions unless stated below.

HSL - This filter is a modified version of the one by Miran.Uhan, the original article can be found here.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

i00

Software Developer (Senior)
i00 Productions
Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionVoting 0... Not going to blindly download PinmemberElkay10:14 24 Feb '12  
AnswerRe: Voting 0... Not going to blindly download Pinmemberi0015:25 27 Feb '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey21:20 23 Feb '12  
AnswerWhy the downloads are broken PinmemberZac Greve4:35 17 Feb '12  
GeneralRe: Why the downloads are broken Pinmemberi002:05 21 Feb '12  
QuestionDownload Broken [Modified] PinmemberZac Greve18:53 16 Feb '12  
SuggestionClarification is not enough. PinmentorMd. Marufuzzaman19:56 7 Feb '12  
GeneralMy vote of 5 Pinmemberdean.armstrong17:47 5 Feb '12  
GeneralMy vote of 3 PinmvpDave Kreskowiak17:24 4 Feb '12  
GeneralRe: My vote of 3 Pinmemberi0017:50 4 Feb '12  
GeneralRe: My vote of 3 PinmvpDave Kreskowiak3:57 5 Feb '12  
GeneralRe: My vote of 3 Pinmemberi0012:34 5 Feb '12  
GeneralRe: My vote of 3 PinmvpDave Kreskowiak16:26 5 Feb '12  
GeneralRe: My vote of 3 [modified] Pinmemberdean.armstrong17:38 5 Feb '12  
GeneralRe: My vote of 3 PinmvpDave Kreskowiak1:32 6 Feb '12  
GeneralRe: My vote of 3 PinmvpDave Kreskowiak1:57 6 Feb '12  
GeneralRe: My vote of 3 Pinmemberi002:08 6 Feb '12  
GeneralRe: My vote of 3 PinmvpDave Kreskowiak3:16 6 Feb '12  
GeneralRe: My vote of 3 Pinmemberi0021:00 5 Feb '12  
GeneralRe: My vote of 3 PinmvpDave Kreskowiak1:33 6 Feb '12  
GeneralRe: My vote of 3 PinmvpDave Kreskowiak2:03 6 Feb '12  
GeneralRe: My vote of 3 PinmvpDave Kreskowiak2:03 8 Feb '12  
QuestionPossible Spyware? PinmemberbinaryDigit@@4:12 26 Jan '12  
AnswerRe: Possible Spyware? Pinmemberi0012:05 26 Jan '12  
GeneralRe: Possible Spyware? Pinmemberi0012:11 29 Jan '12  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 4 Feb 2012
Article Copyright 2012 by i00
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid