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:
public static Bitmap Colorize(Bitmap Image, Color[] Colors)
{
if (Colors.Length < 256)
return null;
Bitmap TempBitmap = new Bitmap(Image.Width, Image.Height);
for (int x = 0; x < Image.Width; ++x)
{
for (int y = 0; y < Image.Height; ++y)
{
int ColorUsing = Image.GetPixel(x, y).R;
TempBitmap.SetPixel(x, y, Colors[ColorUsing]);
}
}
return TempBitmap;
}
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.
b870c63e-f1e9-40c8-8af2-78ca5ef9af5a|3|3.3