Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Sepia Tone in C#

1/26/2009

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#:

   1: /// <summary>
   2: /// Converts an image to sepia tone
   3: /// </summary>
   4: /// <param name="Image">Image to change</param>
   5: /// <returns>A bitmap object of the sepia tone image</returns>
   6: public static Bitmap ConvertSepiaTone(Bitmap Image)
   7: {
   8:     ColorMatrix TempMatrix = new ColorMatrix();
   9:     TempMatrix.Matrix = new float[][]{
  10:                     new float[] {.393f, .349f, .272f, 0, 0},
  11:                     new float[] {.769f, .686f, .534f, 0, 0},
  12:                     new float[] {.189f, .168f, .131f, 0, 0},
  13:                     new float[] {0, 0, 0, 1, 0},
  14:                     new float[] {0, 0, 0, 0, 1}
  15:                 };
  16:     return TempMatrix.Apply(Image);
  17: }

ColorMatrix.cs

   1: /*
   2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
   3: 
   4: Permission is hereby granted, free of charge, to any person obtaining a copy
   5: of this software and associated documentation files (the "Software"), to deal
   6: in the Software without restriction, including without limitation the rights
   7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   8: copies of the Software, and to permit persons to whom the Software is
   9: furnished to do so, subject to the following conditions:
  10: 
  11: The above copyright notice and this permission notice shall be included in
  12: all copies or substantial portions of the Software.
  13: 
  14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20: THE SOFTWARE.*/
  21:  
  22: #region Usings
  23: using System.Drawing;
  24: using System.Drawing.Imaging;
  25: #endregion
  26:  
  27: namespace Utilities.Media.Image
  28: {
  29:     /// <summary>
  30:     /// Helper class for setting up and applying a color matrix
  31:     /// </summary>
  32:     public class ColorMatrix
  33:     {
  34:         #region Constructor
  35:  
  36:         /// <summary>
  37:         /// Constructor
  38:         /// </summary>
  39:         public ColorMatrix()
  40:         {
  41:         }
  42:  
  43:         #endregion
  44:  
  45:         #region Properties
  46:  
  47:         /// <summary>
  48:         /// Matrix containing the values of the ColorMatrix
  49:         /// </summary>
  50:         public float[][] Matrix { get; set; }
  51:  
  52:         #endregion
  53:  
  54:         #region Public Functions
  55:  
  56:         /// <summary>
  57:         /// Applies the color matrix
  58:         /// </summary>
  59:         /// <param name="OriginalImage">Image sent in</param>
  60:         /// <returns>An image with the color matrix applied</returns>
  61:         public Bitmap Apply(Bitmap OriginalImage)
  62:         {
  63:             Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
  64:             using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
  65:             {
  66:                 System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
  67:                 using (ImageAttributes Attributes = new ImageAttributes())
  68:                 {
  69:                     Attributes.SetColorMatrix(NewColorMatrix);
  70:                     NewGraphics.DrawImage(OriginalImage,
  71:                         new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
  72:                         0, 0, OriginalImage.Width, OriginalImage.Height,
  73:                         GraphicsUnit.Pixel,
  74:                         Attributes);
  75:                 }
  76:             }
  77:             return NewBitmap;
  78:         }
  79:  
  80:         #endregion
  81:     }
  82: }

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.



Comments