Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to remove EXIF data from a JPEG with .NET

Tags:

.net

exif

How can I remove all EXIF data from a JPEG image?

I found lots of examples on how to read and edit the EXIF data with various libraries, but all I would need is a simple example on how to remove it.

It is just for testing proposes, so even the ugliest and hackished approach would be helpful :)

I already tried searching for the EXIF start/end markers 0xFFE1 & 0xFFE2. The last one does not exist in my case.

like image 374
marc.d Avatar asked Jun 17 '09 06:06

marc.d


People also ask

How do I remove identifying information from a JPEG?

Right click on the image you want to remove the data from. Click "Properties" Click "Details" Click "Remove Properties and Personal Information"

Can EXIF data be removed?

Select all the files you want to delete EXIF metadata from. Right-click anywhere within the selected fields and choose “Properties.” Click the “Details” tab. At the bottom of the “Details” tab, you'll see a link titled “Remove Properties and Personal Information.” Click this link.


1 Answers

I first wrote about this using WPF libs in my blog, but this sort of failed since Windows backend calls are a bit messed up.

My final solution is also much quicker which basically byte patches the jpeg in order to remove the exif. Fast and simple :)

[EDIT: Blog post has more updated code]

namespace ExifRemover
{
  public class JpegPatcher
  {
    public Stream PatchAwayExif(Stream inStream, Stream outStream)
    {
      byte[] jpegHeader = new byte[2];
      jpegHeader[0] = (byte) inStream.ReadByte();
      jpegHeader[1] = (byte) inStream.ReadByte();
      if (jpegHeader[0] == 0xff && jpegHeader[1] == 0xd8)
      {
        SkipExifSection(inStream);
      }

      outStream.Write(jpegHeader,0,2);

      int readCount;
      byte[] readBuffer = new byte[4096];
      while ((readCount = inStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
        outStream.Write(readBuffer, 0, readCount);

      return outStream;
    }

    private void SkipExifSection(Stream inStream)
    {
      byte[] header = new byte[2];
      header[0] = (byte) inStream.ReadByte();
      header[1] = (byte) inStream.ReadByte();
      if (header[0] == 0xff && header[1] == 0xe1)
      {
        int exifLength = inStream.ReadByte();
        exifLength = exifLength << 8;
        exifLength |= inStream.ReadByte();

        for (int i = 0; i < exifLength - 2; i++)
        {
          inStream.ReadByte();
        }
      }
    }
  }
}
like image 74
Mikael Svenson Avatar answered Oct 12 '22 23:10

Mikael Svenson