Dot-Net

使用 .NET 從 JPEG 中刪除 EXIF 數據的簡單方法

  • November 17, 2018

如何從 JPEG 圖像中刪除所有 EXIF 數據?

我找到了很多關於如何使用各種庫讀取和編輯 EXIF 數據的範例,但我只需要一個關於如何刪除它的簡單範例。

它僅用於測試建議,因此即使是最醜陋和最駭人聽聞的方法也會有所幫助:)

我已經嘗試搜尋 EXIF 開始/結束標記 0xFFE1 和 0xFFE2。最後一個在我的情況下不存在。

我認為將文件讀入點陣圖對象並再次寫入文件應該可以解決問題。

我記得在執行我的“圖像旋轉程序”時感到沮喪,因為它刪除了 EXIF 數據。但在這種情況下,這正是你想要的!

我第一次在我的部落格中使用 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();
       }
     }
   }
 }
}

引用自:https://stackoverflow.com/questions/1005463