I had a requirement to Rotate an image (90, 180, 270 etc..) based on the user input, and also for me the input was stream of bytes. After finishing that task I thought of posting the same in C# corner. So for that what I did is, I took a simple image. Read all bytes in from a file on the disk. Create a memory stream from those bytes. Then I got the image from those memoryStream, and then I rotate the image using RotateFlipType, then I saved the file in some local path.
And now the problem has begun. When I start comparing all the properties for both the images i.e The original image and the rotated one, What I observed is the new modified rotated image has different CompressionValue.
Then I change the logic a bit to retain the same compression value.
I Create an Encoder object based on the GUID. I tried to get the compression type from the image.
And from that compression type got the compression value and set it back after rotating the image.
- /// <summary>
- /// It returns a value indicating the compression type.
- /// 1: no compression
- /// 2: CCITT Group 3
- /// 3: Facsimile-compatible CCITT Group 3
- /// 4: CCITT Group 4 (T.6)
- /// 5: LZW
- /// </summary>
- /// <param name="image">image</param>
- /// <returns>compression type</returns>
- private static int GetCompressionType(Image image)
- {
- int compressionTagIndex = Array.IndexOf(image.PropertyIdList, 0x103);
- PropertyItem compressionTag = image.PropertyItems[compressionTagIndex];
- return BitConverter.ToInt16(compressionTag.Value, 0);
- }
- /// <summary>
- /// Get Compression Value based on Compression Type
- /// </summary>
- /// <param name="getCompressionType"></param>
- /// <returns></returns>
- private long GetCompressionValue(int getCompressionType)
- {
- switch (getCompressionType)
- {
- case 1:
- return (long) EncoderValue.CompressionNone;
- case 2:
- case 3:
- return (long) EncoderValue.CompressionCCITT3;
- case 4:
- return (long) EncoderValue.CompressionCCITT4;
- case 5:
- return (long) EncoderValue.CompressionLZW;
- default:
- throw new NotImplementedException();
- }
- }

Vikas SinghPosted Sep 26, 2014, 3:58 AM
superub
Midhunlal GPosted Sep 25, 2014, 6:21 AM
Nice post. Need to add a validation for image compression attribute. Code is crashing for images without compression attribute. Need to add validation for the same.
Vithal WadjePosted Sep 23, 2014, 1:10 PM
super keep it up