What would be a good TRUE black and white colormatrix?

后端 未结 4 1425
星月不相逢
星月不相逢 2020-12-05 03:50

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?

4条回答
  •  心在旅途
    2020-12-05 04:13

    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);
            }
        }
    }
    

提交回复
热议问题