Click here to Skip to main content
Click here to Skip to main content

VB.NET Image Filters

By , 10 Oct 2012
 
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

'EdgeDetection
'Highlights edges in black and white
b.Filters.EdgeDetection()

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

'Fontify
'Creates an ASCII image (font representation) of an image
b.Filters.Fontify(font As Font, ColorCount As Byte, ColorAlso As Boolean)
'font
'    Specifies the font that is to be used with the ASCII image
'ColorCount
'    Specifies the amount of chars that will be used when generating the final image
'ColorAlso
'    If this value is true each char in the image output will be colored

'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

'OilPainting
'Creates an artistic oil painting impression of an image
b.Filters.OilPainting(Amount As Integer, BleedTransparentEdges As Boolean)
'Amount
'    Specifies the brush size for the oil painting
'BleedTransparentEdges
'    Also applies the oil painting effect to the alpha channel

'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)

'TwoTone
'Gets the average color of each pixel and converts it to either black or white
b.Filters.TwoTone(Amount As Byte)
'Amount
'    The tollance used to determine whether the pixel is black or white

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

20121011

  • GausianBlur now uses WPF rendering for faster results (~11x faster in tests)
  • Added ApplyWPFEffect that allows WPF effects to be used on bitmaps
  • Fixed a bug with the OilPainting filter where it would incorrectly paint transparent sections to parts of the image
  • Changed interface to support saving and added undo
  • Added TwoTone filter
  • Added Fontify filter
  • Added EdgeDetection filter

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
I hope you enjoy my code. It's yours to use for free, but if you do wish to say thank you then a donation is always appreciated.
You can donate here.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionUsing the filters in a new projectmembertomweber31 Oct '12 - 22:50 
Your code is great, but your description on how to get started (in a new project) is lacking something.
 
You say:
 
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
 
Could you perhaps add a description on which files should be included in a new project? When I simply add the "clsImageFilters", VS2010 is crying.
I guess that simply adding "clsImageFilters" is not sufficient because it does not import anything.
AnswerRe: Using the filters in a new projectmembertweber201231 Oct '12 - 23:22 
Using VS2010 and framework 4, I tried the following:
 
I added the following references to a new project:
presentationcore
presentationframework
system.drawing
 
I linked your 2 files "clsImageFilters" and "DrawingFunctions".
 
Compile project.
 
It will tell you that PresentationCore is needed and that a reference should be added to the project.
Click the error code, and accept the suggested change.
It will tell you that System.Windows.Freezable is needed and that a reference should be added to the project.
Click the error code, and accept the suggested change.
It will tell you that XAML is needed and that a reference should be added to the project.
Click the error code, and accept the suggested change.
 
Afterwards, it worked fine.
 
Then, out of curiosity, I removed the reference to the presentationcode and presentationframework and system.drawing, and it still worked.
 
I am baffled now.
GeneralRe: Using the filters in a new projectmemberi001 Nov '12 - 4:10 
tweber2012 wrote:
Then, out of curiosity, I removed the reference to the presentationcode and presentationframework and system.drawing, and it still worked.

 
Hrm interesting ... also what is Freezable?? .Net 4 Thing??
 
Kris

AnswerRe: Using the filters in a new projectmemberi001 Nov '12 - 4:14 
... I will include further details on doing this in the next release...
 
The structure has changed a bit since I am now in a not yet released version ... but from memory add the ImageFilters.vb to your project. Add the references: PresentationCore, PresentationFramework and WindowsBase.
 
Hope this works,
Kris

QuestionSystem.Design missing for me in FrameWork 4membertomweber19 Oct '12 - 7:29 
Hello!
 
Do I really have to use the FrameWork 2 version of it?
 
Thank you!
AnswerRe: System.Design missing for me in FrameWork 4 [modified]memberi0019 Oct '12 - 15:19 
I don't have VS 10 but...
Just because a reference's version is not 3.5 doesn't mean it is not distributed with 3.5 ... a lot of references have not been updated since version 2... and why ... because they were written well enough in the first place that they didn't need to be...
 
When you create a new forms project in 3.5 these are the references that get added:
System 2.0
System.Core 3.5
System.Data 2.0
System.Data.DataSetExtensions 3.5
System.Deployment 2.0
System.Drawing 2.0
System.Windows.Forms 2.0
System.Xml 2.0
System.Xml.Linq 3.5
 
As you can see most of the references that get added with 3.5 are 2.0 references; but are included and used (by default) in 3.5.
 
Also you can use references for newer versions if they are available in VS2010, since you upgraded the project it will tend to use the older versions if they are installed on your computer.
 
Regards,
Kris


modified 20 Oct '12 - 19:09.

QuestionNice code............memberPatil Kishor10 Oct '12 - 7:28 
Good Article keep it up. thank you for sharing a code.........
QuestionSaving Issuememberstorm625 Aug '12 - 19:42 
While I am trying to save image after applying these filters from your source code is show a error that no bpimage.image is not a member of imagefilters.buffered panel.
AnswerRe: Saving Issuememberi0025 Aug '12 - 19:48 
Hi, can you please provide the exact source code you are using .. and note the exact line that the error is occuring on..
 
Thanks
Kris
GeneralRe: Saving Issuememberstorm626 Aug '12 - 17:38 
<pre lang="xml">&lt;pre lang=&quot;vb&quot;&gt;Private Sub SaveToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem1.Click
       Try
           Dim Savefiledialog1 As New SaveFileDialog()
           Savefiledialog1.Filter = &amp;quot;Bitmap Image (*.bmp)|*.bmp |All Files |*.*&amp;quot;
           If Savefiledialog1.ShowDialog() = DialogResult.OK Then
               bpImage.Image.Save(Savefiledialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
 
           End If
       Catch ex As Exception
           MsgBox(&amp;quot;No Image is opened for save&amp;quot;)
       End Try
   End Sub&lt;/pre&gt;</pre>
 
'this is code for saving image this code works on my other projects but not on your this project the error is
'image' is not a member of 'ImageFilters.bufferedPanel'
do you have any idea about this error with your project. I am modifying your code and adding cartoonize effect in this project but before going to start it When I try to save edited image is not working

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 10 Oct 2012
Article Copyright 2012 by i00
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid