使用.NET从JPEG中删除EXIF数据的简单方法

mar*_*c.d 14 .net exif

如何从JPEG图像中删除所有EXIF数据?

我找到了很多关于如何使用各种库来读取和编辑EXIF数据的示例,但我需要的只是一个关于如何删除它的简单示例.

它只是用于测试建议,所以即使是最丑陋和最黑客的方法也会有所帮助:)

我已经尝试搜索EXIF开始/结束标记0xFFE1和0xFFE2.在我的情况下,最后一个不存在.

Mik*_*son 25

我首先在我的博客中使用WPF库来写这个,但是由于Windows后端调用有点搞砸了,这种失败.

我的最终解决方案也快得多,基本上字节补丁jpeg以删除exif.快速而简单:)

[编辑:博客文章有更新的代码]

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();
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这在使用Bitmap进行保存时效果很好,但对我来说却并不奏效。 (2认同)

Dav*_*nde 6

我认为将文件读入Bitmap对象并再次写入文件应该可以解决问题.

我记得在执行"图像旋转程序"时感到沮丧,因为它删除了EXIF数据.但在这种情况下,它正是你想要的!