Sepia Tone in C#

For all of that old-timey fun...
Jan 26 2009 by James Craig

Sepia Tone is the coloring of a black and white photo to simulate early photos (the sort of faded/brownish ones you see from time to time). There aren't that many practical reasons to want to use it but it does give the image an "old time" look. That from a stylistic stand point can be rather cool. And lucky for us, it's extremely easy to implement in C#:

 /// <summary>
/// Converts an image to sepia tone
/// </summary>
/// <param name="Image">Image to change</param>
/// <returns>A bitmap object of the sepia tone image</returns>
public static Bitmap ConvertSepiaTone(Bitmap Image)
{
ColorMatrix TempMatrix = new ColorMatrix();
TempMatrix.Matrix = new float\[\]\[\]{
new float\[\] {.393f, .349f, .272f, 0, 0},
new float\[\] {.769f, .686f, .534f, 0, 0},
new float\[\] {.189f, .168f, .131f, 0, 0},
new float\[\] {0, 0, 0, 1, 0},
new float\[\] {0, 0, 0, 0, 1}
};
return TempMatrix.Apply(Image);
}

ColorMatrix.cs #

 /\*
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 similar to my code for converting an image to black and white. However you'll notice that the matrix is slightly different.  The matrix in this case takes more of the values for red and green while downplaying the blue a bit which in turn gives us the brownish color. That's our only difference really, and since I talked about the color matrix a bit more in the black and white posting I'm going to simply shut up and leave it at that. Anyway, hopefully this helps you out in some way. So try it out, leave feedback, and happy coding.