Download Sourcecode

Introduction

This application uses the gimp software to apply filters to images in C#.

Background

I wanted to use the different filters for images in my desktop application. When searched, I saw a lot of useful filters in Gimp which could be used. Then I needed a way to use those filters in my C# application.

Using the code

First of all you need to install gimp. After that set the path variable(C:\Program Files\GIMP-2.0\bin). Then go to your home directory and find the \.gimp-2.6\scripts folder and copy the example gimp scripts there.Now you can run the application..

private void unsharpMaskButton_Click(object sender, EventArgs e)
{
    try
    {
        if (!string.IsNullOrEmpty(choosenFile))
        {
            int radius = radiusTrackBar.Value;
            int amount = amountTrackBar.Value;
            int threshold = ThresholdTrackBar.Value;
            string path = choosenFile.Replace(@"\", @"\\");
            //creating the command
            string a = @"gimp-2.6.exe -i -b ""(simple-unsharp-mask \""" + 
                   path + @"\"" " + radius + " " + amount + " " + threshold +
                   @")"" -b ""(gimp-quit 0)""";
            //execute gimp filter in command line
            string result = ExecuteCommandSync(a);
            //sets the new image as the pictureBox image
            Bitmap newImage = (Bitmap)Image.FromFile(choosenFile);
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = new Bitmap(newImage);
            newImage.Dispose();
            newImage = null;
        }
        else
        {
            MessageBox.Show("please select a image");
        }
    }
    catch (Exception a)
    {
        MessageBox.Show(a.ToString());
    }
}

Gimp Script for Unsharpmask filter

(define (simple-unsharp-mask filename
radius
amount
threshold)
(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
(drawable (car (gimp-image-get-active-layer image))))
(plug-in-unsharp-mask RUN-NONINTERACTIVE
image drawable radius amount threshold)
(set! drawable (car (gimp-image-get-active-layer image)))
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-image-delete image)))

You can write various gimp scripts.You get all the details about the gimp functions in pluginrc file. You can find the pluginrc file in your home floder(C:\Users\dhanu-sdu\.gimp-2.6).You should save gimp scripts with .scm extension. I think this is a nice way to use various filters for images in C#.