如何在C#中将TIFF与颜色合并?

时间:2019-02-07 11:24:20

标签: c# merge bitmap tiff multipage

我正在做一个合并TIFF文件的事,它确实合并了它们,但是用的是黑白的。如果我有一个彩色文件,它将变成黑色和白色。 我想保留颜色,但是我不知道该怎么做。 首先,它将TIFF转换为Bitmap,并将Bitmap转换为Bitonal。然后它将使用另一个功能保存在新文件中。

我已经搜索过compression types,并且已经将其更改为“ CompressionLZW”,但是它仍然出现在B&W中。 我还更改了位图的PixelFormats,但仍然是黑白结果。

这是我的“ ConvertToBitonal”功能:

    public Bitmap ConvertToBitonal(Bitmap original)
    {
        Bitmap source = null;
        // If original bitmap is not already in 32 BPP, ARGB format, then convert
        if (original.PixelFormat != PixelFormat.Format32bppPArgb)
        {
            source = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppPArgb);
            source.SetResolution(original.HorizontalResolution, original.VerticalResolution);
            using (Graphics g = Graphics.FromImage(source))
            {
                g.DrawImageUnscaled(original, 0, 0);
            }
        }
        else
        {
            source = original;
        }


        // Lock source bitmap in memory
        BitmapData sourceData = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);

        // Copy image data to binary array
        int imageSize = sourceData.Stride * sourceData.Height;
        byte[] sourceBuffer = new byte[imageSize];
        Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, imageSize);

        // Unlock source bitmap
        source.UnlockBits(sourceData);

        // Create destination bitmap
        Bitmap destination = new Bitmap(source.Width, source.Height, PixelFormat.Format1bppIndexed);
        destination.SetResolution(source.HorizontalResolution, source.VerticalResolution);

        // Lock destination bitmap in memory
        BitmapData destinationData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);

        // Create destination buffer
        imageSize = destinationData.Stride * destinationData.Height;
        byte[] destinationBuffer = new byte[imageSize];

        int sourceIndex = 0;
        int destinationIndex = 0;
        int pixelTotal = 0;
        byte destinationValue = 0;
        int pixelValue = 128;
        int height = source.Height;
        int width = source.Width;
        int threshold = 500;

        // Iterate lines
        for (int y = 0; y < height; y++)
        {
            sourceIndex = y * sourceData.Stride;
            destinationIndex = y * destinationData.Stride;
            destinationValue = 0;
            pixelValue = 128;

            // Iterate pixels
            for (int x = 0; x < width; x++)
            {
                // Compute pixel brightness (i.e. total of Red, Green, and Blue values)
                pixelTotal = sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2] + sourceBuffer[sourceIndex + 3];
                if (pixelTotal > threshold)
                {
                    destinationValue += (byte)pixelValue;
                }
                if (pixelValue == 1)
                {
                    destinationBuffer[destinationIndex] = destinationValue;
                    destinationIndex++;
                    destinationValue = 0;
                    pixelValue = 128;
                }
                else
                {
                    pixelValue >>= 1;
                }
                sourceIndex += 4;
            }
            if (pixelValue != 128)
            {
                destinationBuffer[destinationIndex] = destinationValue;
            }
        }

        // Copy binary image data to destination bitmap
        Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize);

        // Unlock destination bitmap
        destination.UnlockBits(destinationData);

        // Return
        return destination;
    }

还有我的“ savemultipage”功能,其中描述了压缩方式:

    public bool saveMultipage(Image[] bmp, string location, string type)
    {
        if (bmp != null)
        {
            try
            {
                ImageCodecInfo codecInfo = getCodecForstring(type);

                for (int i = 0; i < bmp.Length; i++)
                {
                    if (bmp[i] == null)
                        break;
                    bmp[i] = (Image)ConvertToBitonal((Bitmap)bmp[i]);
                }

                if (bmp.Length == 1)
                {

                    EncoderParameters iparams = new EncoderParameters(1);
                    Encoder iparam = Encoder.Compression;
                    EncoderParameter iparamPara = new EncoderParameter(iparam, (long)(EncoderValue.CompressionLZW));
                    iparams.Param[0] = iparamPara;
                    bmp[0].Save(location, codecInfo, iparams);


                }
                else if (bmp.Length > 1)
                {

                    Encoder saveEncoder;
                    Encoder compressionEncoder;
                    EncoderParameter SaveEncodeParam;
                    EncoderParameter CompressionEncodeParam;
                    EncoderParameters EncoderParams = new EncoderParameters(2);

                    saveEncoder = Encoder.SaveFlag;
                    compressionEncoder = Encoder.Compression;

                    // Save the first page (frame).
                    SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.MultiFrame);
                    CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionLZW);
                    EncoderParams.Param[0] = CompressionEncodeParam;
                    EncoderParams.Param[1] = SaveEncodeParam;

                    File.Delete(location);
                    bmp[0].Save(location, codecInfo, EncoderParams);


                    for (int i = 1; i < bmp.Length; i++)
                    {
                        if (bmp[i] == null)
                            break;

                        SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.FrameDimensionPage);
                        CompressionEncodeParam = new EncoderParameter(compressionEncoder, (long)EncoderValue.CompressionLZW);
                        EncoderParams.Param[0] = CompressionEncodeParam;
                        EncoderParams.Param[1] = SaveEncodeParam;
                        bmp[0].SaveAdd(bmp[i], EncoderParams);

                    }

                    SaveEncodeParam = new EncoderParameter(saveEncoder, (long)EncoderValue.Flush);
                    EncoderParams.Param[0] = SaveEncodeParam;
                    bmp[0].SaveAdd(EncoderParams);
                }
                return true;


            }
            catch (System.Exception ee)
            {
                throw new Exception(ee.Message + "  Erro em guardar o ficheiro TIFF multipage!");
            }
        }
        else
            return false;

    } 

如果有人能告诉我代码中要进行哪些更改,以便颜色TIFF文件与其中的颜色保持一致,我将不胜感激。

0 个答案:

没有答案