Red, Blue, and Green Filters in C#

Simple example filters for image processing.
Apr 09 2009 by James Craig

I remember when the Matrix came out, I was really curious how they ended up getting that weird greenish look on everything. I was young, I knew nothing about image processing but in my head I figured it had to be the lighting. I kept thinking "Wow, that must have been really annoying having a greenish light on everything"... Like I said, I was young... Anyway, eventually I found out about green filters (as well as blue and red filters). For those of you that don't know what that is, it's simply taking the green (or red or blue or any color really) component of the image, overlaying it over the original image with a certain opacity to give the image a specific tint.

Now after updating my utility library, I noticed that I had never added that functionality. In fact I never added anything to help you accomplish this if you wanted to create the effect. In order to help, I've added some functions to get the green, red, and blue channels individually and as always I'm going to show you how to do that yourself:

 /// <summary>
/// Gets the Green filter for an image
/// </summary>
/// <param name="Image">Image to change</param>
/// <returns>A bitmap object</returns>
public static Bitmap GreenFilter(Bitmap Image)
{
ColorMatrix TempMatrix = new ColorMatrix();
TempMatrix.Matrix = new float\[\]\[\]{
new float\[\] {0, 0, 0, 0, 0},
new float\[\] {0, 1, 0, 0, 0},
new float\[\] {0, 0, 0, 0, 0},
new float\[\] {0, 0, 0, 1, 0},
new float\[\] {0, 0, 0, 0, 1}
};
return TempMatrix.Apply(Image);
}

 /\*
Copyright (c) 2010 <a href="">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.\*/

#region Usings
using System.Drawing;
using System.Drawing.Imaging;
#endregion

namespace Utilities.Media.Image
{
/// <summary>
/// Helper class for setting up and applying a color matrix
/// </summary>
public class ColorMatrix
{
#region Constructor

/// <summary>
/// Constructor
/// </summary>
public ColorMatrix()
{
}

#endregion

#region Properties

/// <summary>
/// Matrix containing the values of the ColorMatrix
/// </summary>
public float\[\]\[\] Matrix { get; set; }

#endregion

#region Public Functions

/// <summary>
/// Applies the color matrix
/// </summary>
/// <param name="OriginalImage">Image sent in</param>
/// <returns>An image with the color matrix applied</returns>
public Bitmap Apply(Bitmap OriginalImage)
{
Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
{
System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
using (ImageAttributes Attributes = new ImageAttributes())
{
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(OriginalImage,
new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
0, 0, OriginalImage.Width, OriginalImage.Height,
GraphicsUnit.Pixel,
Attributes);
}
}
return NewBitmap;
}

#endregion
}
}

The code above is very similar to the sepia tone and black and white code that I've shown earlier. This code uses a ColorMatrix with both the blue and red channels ignored (only focusing on the green channel). We can switch this to the red or blue channel or any combination of them that you want. After the ColorMatrix is created, it (well GDI+) just does a pass on the image using that filter. In return it spits back all green that is found in the image. Past that, there isn't much to the code itself.

Now if you try out the code, you'll notice that it's VERY green. I don't remember people in the Matrix looking like that, so how do we give our images the same look? As I said above, we need to overlay the image we get from here over the original image with a specific opacity. In order to do this, I'm going to use the Watermark function from my utility library:

 Bitmap TempImage = Utilities.Media.Image.Image.GreenFilter(ORIGINAL IMAGE LOCATION);
Bitmap TempImage2 = (Bitmap)Bitmap.FromFile(ORIGINAL IMAGE LOCATION);
Bitmap TempImage3 = Utilities.Media.Image.Image.Watermark(TempImage2, TempImage, 0.2f, 0, 0, Color.FromArgb(255, 0, 255));
TempImage.Dispose();
TempImage2.Dispose();
TempImage3.Save(FINAL IMAGE LOCATION);
TempImage3.Dispose();

You'll have to update the location of the original and final image locations but that's it. Doing the above creates an image with a light green filter similar to what was done in the Matrix but definitely play with the opacity to get the effect that you want. Anyway, I hope this helps you out, so give it a try, leave feedback, and happy coding.