Image/ByteArray Compression Conversion to CCITT T.6 or Group 4

Hi,

I have tried several different things but it seems everything I do the image that was saved has a compression of LZW.

All I have to work with is a byte array. I don't have an image file only a byte array and one of the things I do is save that byte array as image file to see if the compression is changed. It's not.

Even though I am only working with one image I still put it into a List<byte[]>();

I then call a method:

        public byte[] ConvertImage (List<byte[]> aryImgByte, TIFFCompression objCompType)
        {
            FileUtils objFileUtil = new FileUtils();
            string strSentFileLog = string.Empty;

            EncoderValue ev = new EncoderValue();
            switch (objCompType)
            {
                case TIFFCompression.None:
                    ev = EncoderValue.CompressionNone;
                    break;
                case TIFFCompression.RLE:
                    ev = EncoderValue.CompressionRle;
                    break;
                case TIFFCompression.LZW:
                    ev = EncoderValue.CompressionLZW;
                    break;
                case TIFFCompression.CCITT3:
                    ev = EncoderValue.CompressionCCITT3;
                    break;
                case TIFFCompression.CCITT4:
                    ev = EncoderValue.CompressionCCITT4;
                    break;
                default:
                    ev = EncoderValue.CompressionNone;
                    break;
            }

            // If the list is null or empty, return null
            if ((aryImgByte == null) || (aryImgByte.Count == 0))
            {
                return null;
            }

            //set up encoder and data streams
            ImageCodecInfo codecInfo = GetCodec("image/tiff");

            Encoder enc = Encoder.SaveFlag;
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = new EncoderParameter(enc, (long)ev);
            Bitmap pages = null;
            int frame = 0;

            MemoryStream imageStream;
            MemoryStream outStream = new MemoryStream();

            // Add each image as a page in the TIFF
            foreach (byte[] image in aryImgByte)
            {
                imageStream = new MemoryStream(image);

                if (frame == 0)
                {
                    // Save the first frame
                    pages = (Bitmap)Image.FromStream(imageStream);
                    pages.Save(outStream, codecInfo, encoderParams);
                }
                else
                {
                    // Save intermediate frames
                    encoderParams.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                    Bitmap page = (Bitmap)Image.FromStream(imageStream);
                    pages.SaveAdd(page, encoderParams);
                }

                imageStream.Close();
                frame++;
            }

            byte[] imageBytes = outStream.ToArray();

            outStream.Close();

            // Return the final image
            return imageBytes;

        }

Open in new window


I call it with a:

aryTempBuff = objImageUtil.ConvertImage(lstImage, TIFFCompression.CCITT4);

Open in new window


I would pass It along as a byte array too but I wanted to save the file to make sure it has the appropriate compression.

So, I do a File.WriteAllBytes({location and file}, the bytearray).

When I go to read the file I do a:

                Stream MyMS = new MemoryStream(ms.ToArray());
                Image returnImage = Image.FromStream(MyMS);
                returnImage.Save("C:\\MyTest\\Test\\Test01" + "\\myImage.tiff", System.Drawing.Imaging.ImageFormat.Tiff);

Open in new window


The image has the file details of this:ImageCompTest.rtf

Any ideas/suggestions on how I get that compression changed?

Any info would be greatly appreciated.
LVL 1
davismAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
This worked for me:

byte[] imgBits = File.ReadAllBytes(@"F:\Downloads\multipage_tif_example.tif");

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("out.tif", codec, parms);
    }
}

Open in new window


Screenshot
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davismAuthor Commented:
It turns out it the Bitmap reference I had:

pages = (Bitmap)Image.FromStream(imageStream);

Needed to be:

pages = Image.FromStream(imageStream);

And the definition of "pages" needed to be image and not Bitmap.

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.