Removing EXIF Data From Images With .NET
Written by Ben McInturff   
Friday, 07 August 2009 14:31
I recently had a requirement to remove EXIF data from images so that we could improve the request delivery by decreasing total bytes transmitted across the wire. EXIF is the form that image metadata takes, and with small images, the transmission savings may be significant.

 

If you want to see what EXIF data looks like, right click on an image (assuming you are a windows user) and select properties->details. The information you see is all EXIF data, and it can be removed from images before they are served. It can also be useful if you want to remove any personal or identifying info that your camera may expose on the internet without you knowing it. 

There is also an example of using an encoding codec to change the compression of the image. 

Anyway, the code I have for removing the EXIF data (which I couldn't easily find anywhere else on the internet) is this:

System.IO.MemoryStream imageStream 
    = new System.IO.MemoryStream(image);
System.IO.MemoryStream newImageStream
= new System.IO.MemoryStream();
System.Drawing.Image cleanImage 
    = System.Drawing.Image.FromStream(imageStream);
 System.Drawing.Imaging.EncoderParameter encoderQuality
    = new System.Drawing.Imaging.EncoderParameter(
    System.Drawing.Imaging.Encoder.Quality,35) 
    );
System.Drawing.Imaging.EncoderParameters encoderParams = 
    new System.Drawing.Imaging.EncoderParameters(1);
encoderParams.Param[0] = encoderQuality;
System.Drawing.Imaging.ImageCodecInfo[] iciCodecs = 
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo iciJpegCodec = null;
for (int i = 0; i < iciCodecs.Length; i++)
{
     // Until the one that we are interested
     // in is found, which is image/jpeg
     if (iciCodecs[i].MimeType == mime)
     {
          iciJpegCodec = iciCodecs[i];
          break;
      }
}
//remove all exif data
foreach (System.Drawing.Imaging.PropertyItem item in cleanImage.PropertyItems)
 {
     cleanImage.RemovePropertyItem(item.Id);
}
cleanImage.Save(newImageStream, iciJpegCodec, encoderParams);
 

 

Valid XHTML and CSS.