简体   繁体   中英

Using .net c# to convert image from jpg to a specific TIFF format

I am trying to send images to a AS-400 and it only accepts TIFF images. I am converting them but then it is complaining that

The error is complaining about "unrecognized tiff tags..." the first two are 317 & 531 Also: "For error codes X'8F0E' and X'8F0F', a bit pattern was detected that does not conform to the rules of the decompression algorithm. Further decompression is not possible. Verify the data integrity of the input data stream and try the request again."

I have a tiff file that works, this is the details of it: 在此处输入图片说明

I am using code off of MSDN that I have modified. the below code IS working, but I now need to have more than 1 parameter in the encoder.

                Bitmap myBitmap;
                ImageCodecInfo myImageCodecInfo;
                Encoder myEncoder;
                EncoderParameter myEncoderParameter;
                EncoderParameters myEncoderParameters;

                // Create a Bitmap object based on a BMP file.
                myBitmap = new Bitmap(@"f:\testFromBlob.jpg");
                // Get an ImageCodecInfo object that represents the TIFF codec.
                myImageCodecInfo = GetEncoderInfo("image/tiff");
                //do the actual work
                myEncoder = Encoder.Compression;
                myEncoderParameters = new EncoderParameters(1);
                myEncoderParameter = new EncoderParameter(myEncoder,(long)EncoderValue.CompressionCCITT4);
                myEncoderParameters.Param[0] = myEncoderParameter;
                myBitmap.Save(@"f:\resultFromDotNet.tiff", myImageCodecInfo, myEncoderParameters);


  private static ImageCodecInfo GetEncoderInfo(String mimeType)
            {
                int j;
                ImageCodecInfo[] encoders;
                encoders = ImageCodecInfo.GetImageEncoders();
                for (j = 0; j < encoders.Length; ++j)
                {
                    if (encoders[j].MimeType == mimeType)
                        return encoders[j];
                }
                return null;
            }

I am out of my depth with image files. Can anybody tell me how to do a conversion that matches the settings from my test image?

I have tried changing the bit depth to 1 as well as the compression, not sure but I think it may need both changed.

UPDATE - using Magick I am able to convert to a working format using

magick convert image01.jpg -compress Group4 tiff3.tiff

If that helps at all getting me on the right track for C# / .Net

Update 2: the above code is working but I need to know how to change multiple parameters vs just one. I think that will probably be the home run.

UPDATE 3: I have the multiple parameters working, posting it here in case it helps somebody else. Now just need to wait for the client to come in and see if this works for them!

For reference here is the link to the MS documentation

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.imaging.encoder.colordepth?view=netframework-4.8

            Bitmap myBitmap;
        ImageCodecInfo myImageCodecInfo;
        Encoder compressionEncoder;
        Encoder colorDepthEncoder;
        EncoderParameter compressionParameter;
        EncoderParameter colorDepthParameter;
        EncoderParameters myEncoderParameters;

        // Create a Bitmap object based on a BMP file.
        myBitmap = new Bitmap(@"f:\colorTest.jpg");

        // Get an ImageCodecInfo object that represents the TIFF codec.
        myImageCodecInfo = GetEncoderInfo("image/tiff");
        //do the actual work
        compressionEncoder = Encoder.Compression;
        myEncoderParameters = new EncoderParameters(2);
        compressionParameter = new EncoderParameter(compressionEncoder,(long)EncoderValue.CompressionCCITT4);

        colorDepthEncoder = Encoder.ColorDepth;
        colorDepthParameter = new EncoderParameter(colorDepthEncoder, 24L); //if needed this can be removed

        myEncoderParameters.Param[0] = compressionParameter;
        myEncoderParameters.Param[1] = colorDepthParameter;
        myBitmap.Save(@"f:\resultFromDotNet1bit.tiff", myImageCodecInfo, myEncoderParameters);

在您的原始代码示例中,您需要将参数 Encoder.Compression 设置为 EncoderValue.CompressionCCITT4,而不是 Encoder.ColorDepth。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM