Rotate an Image Based on the User Input

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.   
  18. /// <summary>  
  19. /// Get Compression Value based on Compression Type  
  20. /// </summary>  
  21. /// <param name="getCompressionType"></param>  
  22. /// <returns></returns>  
  23. private long GetCompressionValue(int getCompressionType)  
  24. {  
  25.     switch (getCompressionType)  
  26.     {  
  27.         case 1:  
  28.             return (long) EncoderValue.CompressionNone;  
  29.   
  30.         case 2:  
  31.         case 3:  
  32.             return (long) EncoderValue.CompressionCCITT3;  
  33.   
  34.         case 4:  
  35.             return (long) EncoderValue.CompressionCCITT4;  
  36.               
  37.         case 5:  
  38.             return (long) EncoderValue.CompressionLZW;  
  39.           
  40.         default:  
  41.             throw new NotImplementedException();  
  42.     }  
  43. }  
Please find the attached code to get the details.