tags:

views:

811

answers:

4

how can i remove all EXIF data from a JPEG, i found lots of examples how to read and edit the EXIF data with various libaries but all i would need is a simple example how to remove it.

it just for testing propose 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 0_o

tia

A: 

If it's only the one file have you tried reading into a paint package such as Paint.NET or Paint Shop Pro and resaving it as a new file?

You might need to copy the image to a new file first.

Of course if there are lots of files then this might not be the best approach ;). Though having said that Paint Shop Pro has a batch conversion option which might be worth investigating.

EDIT: It still might be worth investigating batch operations in paint packages (rather than photo manipulation packages as these are more likely to preserve EXIF data) and see if they can be automated. You can then write a script/batch file to process your images.

iik's comment about reading and writing the file degrading the image will apply in this case, but can be minimised by writing with minimal compression.

ChrisF
unfortunately it is every photo taken with that camera model and maybe also with other models
marc.d
Have you contacted the camera manufacturer - sounds like a bug in their firmware.
ChrisF
@ChrisF: Yeah, that's really gonna solve the problem. :P
Dave Van den Eynde
But they might be aware of it and suggest a workaround. Sorry, I should have put that in my original comment.
ChrisF
"but can be minimised by writing with minimal compression." actually, this is true, but you'll get a file that's bigger than the original.
Dave Van den Eynde
+3  A: 

I think reading in the file into a Bitmap object and writing out to a file again should do the trick.

I remember feeling frustrated while doing my "image rotation program" that it removed the EXIF data. But in this case, it's exactly what you want!

Dave Van den Eynde
i`v gone with this approach for now, thx.
marc.d
+1  A: 

what you should avoid is to decode and re-encode your images because this will hurt the quality. instead you should find a way to modify only the metadata. i haven't tried it but i think InPlaceBitmapMetadataWriter will do the trick.

iik
You're right that decoding/re-encoding will hurt quality (to some extent). I think it's up to the poster to decide if it's worth the effort based upon what he needs.
Dave Van den Eynde
i looked into it thx, but it`s way to complicated for my use case.
marc.d
+4  A: 

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.WriteByte(0xff);
      outStream.WriteByte(0xd8);

      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();
        }
      }
    }
  }
}
Mikael Svenson