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.
-
-
-
-
-
-
-
-
-
-
- private static int GetCompressionType(Image image)
- {
- int compressionTagIndex = Array.IndexOf(image.PropertyIdList, 0x103);
- PropertyItem compressionTag = image.PropertyItems[compressionTagIndex];
- return BitConverter.ToInt16(compressionTag.Value, 0);
- }
-
-
-
-
-
-
- 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();
- }
- }
Please find the attached code to get the details.