Image Filters Using C# .NET
Filtering is one of the most common tasks performed on an image. All methods require at least one argument, ImageID
, to identify the image where the filter is applied. For some methods, it’s also necessary to set the KernelSize
parameter, which specifies the pixel size of the area where the filtering operation is performed.
To apply any filter method to a specific part of an image, use the SetROI
method. If you don’t use the setROI
method, the entire image will be filtered.
Some methods require additional parameters that are specific to the applied filtering operation.
List of All Available Filtering Methods
- Bitonal images:
- Regular images:
- Blur:
- Color dropout
- Colorize
- Connected contour effect
- Contour effect
- Contrast histogram stretch
- Convert to black and white:
- Convolution
- Despeckle noise:
- Diffuse:
- Dilate:
- Edge detection:
- Edge enhancement
- Emboss:
- Intensified emboss:
- Engrave:
- Intensified engrave:
- Equalize
- Erode
- Fire effect
- Grayscale
- Halo effect
- Mirror effect
- Negative effect (color inversion)
- Pixelize
- Red eyes correction
- Relief effect
- Remove chromatographic key
- Scan line effect
- Sepia
- Sharpen:
- Smooth
- Soften
- Subtract background (rolling ball algorithm)
- Swirl effect
- Twirl effect
- Waves effect:
Specifying the Affected Area
To apply a filter to part of an image, specify the area where you want to perform the filtering operation using the SetROI
method. This method takes the following parameters:
-
The coordinates of the top-left corner of the affected area.
-
The width of the affected area.
-
The height of the affected area.
After applying the filter, remember to reset the affected area with the ResetROI
method.
The origin of the coordinate system is the top left-corner of the original image.
To blur an area of 500×500 pixels with the top and left margins set to 100 pixels, use the following code:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Set the affected area. gdpictureImaging.SetROI(100, 100, 500, 500); // Blur the affected area. gdpictureImaging.FxBlur(imageID); // Reset the affected area. gdpictureImaging.ResetROI(); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Set the affected area. gdpictureImaging.SetROI(100, 100, 500, 500) ' Blur the affected area. gdpictureImaging.FxBlur(imageID) ' Reset the affected area. gdpictureImaging.ResetROI() gdImage.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID) End Using
Related Topics
Parameters Specific to Filters
Some filtering methods require additional parameters to specify how the filtering operation is performed. One of those methods is the FxEmboss
method. In addition to the required ImageID
parameter, this method also accepts the BackColor
parameter, which specifies the dominant color of the output image. The following code embosses the image and sets the dominant color to bisque:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg"); // Emboss the image and set the color to bisque. gdpictureImaging.FxEmboss(imageID, Color.Bisque); gdpictureImaging.SaveAsPNG(imageID, @"C:\temp\output.png"); gdpictureImaging.ReleaseGdPictureImage(imageID);
Dim gdpictureImaging As New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Emboss the image and set the color to bisque. gdpictureImaging.FxEmboss(imageID, Color.Bisque) gdpictureImaging.SaveAsPNG(imageID, "C:\temp\output.png") gdpictureImaging.ReleaseGdPictureImage(imageID)