I have a function that will take an image and then return that image in 8-bit color. Almost all of the code was taken from MSDN.
The code is quite short compared to all of the other methods I've seen for converting an image to 8-bit color. I'm just curious as to if this is the most efficient method?
private static Image ImageTo8bpp(Image image)
{
var bitmap = new Bitmap(image);
var imageCodecInfo = GetEncoderInfo("image/tiff");
var encoder = Encoder.ColorDepth;
var encoderParameters = new EncoderParameters(1);
var encoderParameter = new EncoderParameter(encoder, 8L);
encoderParameters.Param[0] = encoderParameter;
var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, imageCodecInfo, encoderParameters);
return Image.FromStream(memoryStream);
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
var imageEncoders = ImageCodecInfo.GetImageEncoders();
return imageEncoders.FirstOrDefault(t => t.MimeType == mimeType);
}