Andy

Andy

  • NA
  • 1
  • 17.6k

Bitmap.RotateFlip resizing image

Jan 29 2011 8:39 PM
I'm iterating over images in a folder and rotating them using the RotateFlip method of the Bitmap class.  The images start out at around 6MB before rotation, but end up 1.32MB after.  Here's a snipet of the code I'm using:
  1. private void ProcessFile(FileInfo fi)  
  2.       {  
  3.         Bitmap bmp = new Bitmap(fi.FullName);  
  4.         EXIFextractor exif = new EXIFextractor(ref bmp, "\n");  
  5.         if (exif["Orientation"] != null)  
  6.         {  
  7.             string orientation = exif["Orientation"].ToString();  
  8.             RotateFlipType rotateFlipType = OrientationToFlipType(orientation);  
  9.             if (rotateFlipType != RotateFlipType.RotateNoneFlipNone)  
  10.             {  
  11.                 //rotate the image  
  12.                 bmp.RotateFlip(rotateFlipType);  
  13.                 if (chkUpdateOrientationProperty.Checked)  
  14.                 {  
  15.                     try  
  16.                     {  
  17.                         //reset the orientation flag  
  18.                         exif.SetOrientationTag(1);  
  19.                     }  
  20.                     catch { }  
  21.                 }  
  22.                 //save the image  
  23.                 string newFileName = (chkRotateOriginals.Checked) ? fi.FullName : SaveAsFileName(fi);  
  24.                 bmp.Save(newFileName, ImageFormat.Jpeg);  
  25.             }  
  26.         }  
  27.         else  
  28.         {  
  29.         }  
  30.         bmp.Dispose();  
  31.     } 
 
If I comment out the line 
bmp.RotateFlip( rotateFlipType );
the image size is not affected, so I've narrowed down the problem to that one line of code.  I've checked the properties of the original and rotated images and the dimensions aren't any smaller (just rotated), dpi, bit depth, etc are all the same.  So I'm at a loss as to why the rotated image's file size is so much smaller.  Does anyone have any idea what would cause this and if there is a way to prevent this from happening?  Thanks in advance.