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

发布于 2024-07-24 23:21:09 字数 215 浏览 6 评论 0原文

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

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

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

我已经尝试搜索 EXIF 开始/结束标记 0xFFE1 和 0xFFE1 。 0xFFE2。 最后一个在我的例子中不存在。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

壹場煙雨 2024-07-31 23:21:09

我首先在我的博客中使用 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();
        }
      }
    }
  }
}

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();
        }
      }
    }
  }
}
末骤雨初歇 2024-07-31 23:21:09

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

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

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!

傲鸠 2024-07-31 23:21:09

你应该避免的是解码和重新编码你的图像,因为这会损害质量。 相反,您应该找到一种仅修改元数据的方法。 我还没有尝试过,但我认为 InPlaceBitmapMetadataWriter 就可以了。

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.

抹茶夏天i‖ 2024-07-31 23:21:09

这个问题已经很久了,但最近遇到了一个问题。 这两个函数将从以文件路径或 byte[] 数组形式提供的 JPG 中删除 EXIF 数据,而不需要任何第三方包。

internal static class ExifRemover
{
    internal static byte[] RemoveExifDataFromImage(string filePath)
    {
        using (var originalImage = Image.FromFile(filePath))
        {
            return Copy(originalImage);
        }
    }
    internal static byte[] RemoveExifDataFromImage(byte[] imageBytes)
    {
        using (var ms = new MemoryStream(imageBytes))
        using (var originalImage = Image.FromStream(ms))
        {
            return Copy(originalImage);
        }
    }
    private static byte[] Copy(Image originalImage)
    {
        using (var newImage = new Bitmap(originalImage.Width, originalImage.Height))
        using (var g = Graphics.FromImage(newImage))
        {
            g.DrawImage(originalImage, 0, 0, originalImage.Width, originalImage.Height);
            var b = new Bitmap(newImage);
            var converter = new ImageConverter();
            return (byte[])converter.ConvertTo(b, typeof(byte[]));
        }
    }
}

它的工作原理是将图像复制到新的位图。 使用返回的字节数组,您可以调用 context.Response.BinaryWrite 将图像写入响应。

Been a long time since this question but ran into an issue with this recently. These two functions will remove EXIF data from JPG's provided either as a filepath or byte[] array, without needing any third party packages.

internal static class ExifRemover
{
    internal static byte[] RemoveExifDataFromImage(string filePath)
    {
        using (var originalImage = Image.FromFile(filePath))
        {
            return Copy(originalImage);
        }
    }
    internal static byte[] RemoveExifDataFromImage(byte[] imageBytes)
    {
        using (var ms = new MemoryStream(imageBytes))
        using (var originalImage = Image.FromStream(ms))
        {
            return Copy(originalImage);
        }
    }
    private static byte[] Copy(Image originalImage)
    {
        using (var newImage = new Bitmap(originalImage.Width, originalImage.Height))
        using (var g = Graphics.FromImage(newImage))
        {
            g.DrawImage(originalImage, 0, 0, originalImage.Width, originalImage.Height);
            var b = new Bitmap(newImage);
            var converter = new ImageConverter();
            return (byte[])converter.ConvertTo(b, typeof(byte[]));
        }
    }
}

It works by copying the image to a new bitmap. With the returned byte array, you can call context.Response.BinaryWrite to write the image to the response.

别理我 2024-07-31 23:21:09

这太简单了,从这里使用 jhead.exe: http://www.sentex.net/~mwandel /jhead/

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

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

It's too easy, use jhead.exe from here: http://www.sentex.net/~mwandel/jhead/

Make a little batch file if you want e.g.:
jhead.exe -purejpg *.jpg

It will strip all metadata from all jpegs in the same folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文