I want to convert an image from color to B/W (i.e. no grayscale, just black and white). Does anyone have a good colormatrix to achieve this?
You dont need a color matrix to achive this, just simply change encoding to CCITT! That only Black & White. Result remains correct and result file size is very small. Also much more efficient and faster than System.DrawImage.
This is the perfect solution:
public void toCCITT(string tifURL)
{
byte[] imgBits = File.ReadAllBytes(tifURL);
using (MemoryStream ms = new MemoryStream(imgBits))
{
using (Image i = Image.FromStream(ms))
{
EncoderParameters parms = new EncoderParameters(1);
ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders()
.FirstOrDefault(decoder => decoder.FormatID == ImageFormat.Tiff.Guid);
parms.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
i.Save(@"c:\test\result.tif", codec, parms);
}
}
}