0
Reply

Bitmap.RotateFlip resizing image

Andy

Andy

Jan 29 2011 8:39 PM
14k
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:
private void ProcessFile( FileInfo fi )
{
	Bitmap bmp = new Bitmap( fi.FullName );
	EXIFextractor exif = new EXIFextractor( ref bmp, "\n" );

	if ( exif["Orientation"] != null )
	{
		string orientation = exif["Orientation"].ToString( );
		RotateFlipType rotateFlipType = OrientationToFlipType( orientation );

		if ( rotateFlipType != RotateFlipType.RotateNoneFlipNone )
		{
			//rotate the image
			bmp.RotateFlip( rotateFlipType );
			if ( chkUpdateOrientationProperty.Checked )
			{
				try
				{
					//reset the orientation flag
					exif.SetOrientationTag( 1 );
				}
				catch { }
			}

			//save the image
			string newFileName = ( chkRotateOriginals.Checked ) ? fi.FullName : SaveAsFileName( fi );
			bmp.Save( newFileName, ImageFormat.Jpeg );
		}

	}
	else
	{
		
	}

	bmp.Dispose( );
}

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.