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#

Colorizing a Black and White Image in C#


I've shown in the past how easy it is to convert an image to black and white. That's a fairly useful technique, but what if we wanted to take a black and white image and convert it back to color? Well to be honest, converting an image to color from black and white is not a perfect one for one matching. For instance, if we use the reverse of the .3, .59, .11 conversion we would end up with an extremely ugly image. So our best bet is to use the black and white colors as a palette with colors that we define ourselves. So lets take a look at what we need to do:

   1: public static Bitmap Colorize(Bitmap Image, Color[] Colors)
   2: {
   3:     if (Colors.Length < 256)
   4:         return null;
   5:     Bitmap TempBitmap = new Bitmap(Image.Width, Image.Height);
   6:     for (int x = 0; x < Image.Width; ++x)
   7:     {
   8:         for (int y = 0; y < Image.Height; ++y)
   9:         {
  10:             int ColorUsing = Image.GetPixel(x, y).R;
  11:             TempBitmap.SetPixel(x, y, Colors[ColorUsing]);
  12:         }
  13:     }
  14:     return TempBitmap;
  15: }

The function takes in an array of colors (256) and uses that to determine the color for each pixel. That's it really. All we have to do is determine what colors we want to use for that array. So try it out, leave feedback, and happy coding.