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.
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)
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
Using b As New Bitmap(128, 128)
Using g = Graphics.FromImage(b)
g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
g.DrawEllipse(Pens.Red, New Rectangle(10, 10, 108, 108))
e.Graphics.DrawImage(b, New Point(0, 0))
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:
b.Filters.Alpha(Amount As Byte)
b.Filters.AlphaMask(AlphaColor As Color, SolidColor As Color)
b.Filters.Brightness(amount As Single)
b.Filters.DropShadow(ShadowColor As Color, Depth As Point, BlurAmount As Integer)
b.Filters.Fontify(font As Font, ColorCount As Byte, ColorAlso As Boolean)
b.Filters.GausianBlur(Amount As Integer)
b.Filters.HSL(hue As Single, sat As Single , lum As Single)
b.Filters.OilPainting(Amount As Integer, BleedTransparentEdges As Boolean)
b.Filters.Static(Amount As Integer)
b.Filters.TwoTone(Amount As Byte)
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.
Downloads per day:
- 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
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.