代码之家  ›  专栏  ›  技术社区  ›  marc.d

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

  •  13
  • marc.d  · 技术社区  · 16 年前

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

    这只是为了测试提议,所以即使是最丑陋、最刻薄的方法也会有帮助:)

    5 回复  |  直到 7 年前
        1
  •  27
  •   Louis Somers    7 年前

    我第一次在我的博客中使用WPF库写了这篇文章,但这类文章失败了,因为Windows后端调用有点混乱。

    我的 final solution 也更快,基本上是字节补丁的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();
            }
          }
        }
      }
    }
    
        2
  •  6
  •   Dave Van den Eynde    16 年前

    我认为将文件读入位图对象并再次将其写入文件应该可以做到这一点。

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

        3
  •  0
  •   Nick    16 年前

    你应该避免的是解码和重新编码你的图像,因为这会损害质量。相反,您应该找到一种只修改元数据的方法。我还没试过,但我想 InPlaceBitmapMetadataWriter 我会成功的。

        4
  •  0
  •   EdChum Arthur G    14 年前

    http://www.sentex.net/~mwandel/jhead/

    如果需要,制作一个小批量文件,例如: jhead.exe -purejpg *.jpg

    它将从同一文件夹中的所有JPEG中删除所有元数据。

    推荐文章