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.

  1. /// <summary>
  2. /// It returns a value indicating the compression type.
  3. /// 1: no compression
  4. /// 2: CCITT Group 3
  5. /// 3: Facsimile-compatible CCITT Group 3
  6. /// 4: CCITT Group 4 (T.6)
  7. /// 5: LZW
  8. /// </summary>
  9. /// <param name="image">image</param>
  10. /// <returns>compression type</returns>
  11. private static int GetCompressionType(Image image)
  12. {
  13. int compressionTagIndex = Array.IndexOf(image.PropertyIdList, 0x103);
  14. PropertyItem compressionTag = image.PropertyItems[compressionTagIndex];
  15. return BitConverter.ToInt16(compressionTag.Value, 0);
  16. }
  17. /// <summary>
  18. /// Get Compression Value based on Compression Type
  19. /// </summary>
  20. /// <param name="getCompressionType"></param>
  21. /// <returns></returns>
  22. private long GetCompressionValue(int getCompressionType)
  23. {
  24. switch (getCompressionType)
  25. {
  26. case 1:
  27. return (long) EncoderValue.CompressionNone;
  28. case 2:
  29. case 3:
  30. return (long) EncoderValue.CompressionCCITT3;
  31. case 4:
  32. return (long) EncoderValue.CompressionCCITT4;
  33. case 5:
  34. return (long) EncoderValue.CompressionLZW;
  35. default:
  36. throw new NotImplementedException();
  37. }
  38. }
Please find the attached code to get the details.