Advertisement
Willcode4cash

Strip EXIF from JPG

May 17th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. public class JpegExtensions
  2. {
  3.     public static Stream StripExif(Stream inStream, Stream outStream)
  4.     {
  5.         byte[] jpegHeader = new byte[2];
  6.         jpegHeader[0] = (byte)inStream.ReadByte();
  7.         jpegHeader[1] = (byte)inStream.ReadByte();
  8.        
  9.         if (jpegHeader[0] == 0xff && jpegHeader[1] == 0xd8) //check if it's a jpeg file
  10.         {
  11.             SkipHeader(inStream);
  12.         }
  13.        
  14.         outStream.WriteByte(0xff);
  15.         outStream.WriteByte(0xd8);
  16.  
  17.         int readCount;
  18.         byte[] readBuffer = new byte[4096];
  19.         while ((readCount = inStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
  20.             outStream.Write(readBuffer, 0, readCount);
  21.  
  22.         return outStream;
  23.     }
  24.  
  25.     private static void SkipHeader(Stream inStream)
  26.     {
  27.         byte[] header = new byte[2];
  28.         header[0] = (byte)inStream.ReadByte();
  29.         header[1] = (byte)inStream.ReadByte();
  30.  
  31.         while (header[0] == 0xff && (header[1] >= 0xe0 && header[1] <= 0xef))
  32.         {
  33.             int exifLength = inStream.ReadByte();
  34.             exifLength = exifLength << 8;
  35.             exifLength |= inStream.ReadByte();
  36.  
  37.             for (int i = 0; i < exifLength - 2; i++)
  38.             {
  39.                 inStream.ReadByte();
  40.             }
  41.            
  42.             header[0] = (byte)inStream.ReadByte();
  43.             header[1] = (byte)inStream.ReadByte();
  44.         }
  45.         inStream.Position -= 2; //skip back two bytes
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement