private static byte[] FixExifData(byte[] originalJpegData)
 {
     using (JpegDecoder jpegDecoder = new JpegDecoder())
     {
         using (ExifExtractor exifExtractor = new ExifExtractor())
         {
             using (ExifBuilder exifBuilder = new ExifBuilder())
             {
                 using (new JpegEncoder())
                 {
                     int        outOffset;
                     byte[]     out1;
                     JpegStatus exifData1 = jpegDecoder.GetExifData(out outOffset, out out1, originalJpegData);
                     if (exifData1 == JpegStatus.WrongFormat)
                     {
                         return(originalJpegData);
                     }
                     IconConverter.ThrowExceptionIfJpegResultFail(exifData1);
                     Exif out2;
                     IconConverter.ThrowExceptionIfJpegResultFail(exifExtractor.Parse(out out2, out1));
                     out2.Thumbnail = (byte[])null;
                     IconConverter.ThrowExceptionIfJpegResultFail(jpegDecoder.SetJpegData(originalJpegData));
                     Size dimension = jpegDecoder.GetDimension();
                     IconConverter.ThrowExceptionIfJpegResultFail(exifBuilder.SetExif(out2));
                     byte[] exifData2 = exifBuilder.GetExifData(dimension.Width, dimension.Height);
                     byte[] numArray  = new byte[originalJpegData.Length - out1.Length + exifData2.Length];
                     int    num1      = 0;
                     Array.Copy((Array)originalJpegData, (Array)numArray, outOffset);
                     int destinationIndex1 = num1 + outOffset;
                     Array.Copy((Array)exifData2, 0, (Array)numArray, destinationIndex1, exifData2.Length);
                     int destinationIndex2 = destinationIndex1 + exifData2.Length;
                     Array.Copy((Array)originalJpegData, outOffset + out1.Length, (Array)numArray, destinationIndex2, originalJpegData.Length - (outOffset + out1.Length));
                     int num2  = exifData2.Length - out1.Length;
                     int index = outOffset - 8;
                     int num3  = ((int)originalJpegData[index] << 8) + (int)originalJpegData[index + 1] + num2;
                     numArray[index]     = (byte)(num3 >> 8);
                     numArray[index + 1] = (byte)num3;
                     return(numArray);
                 }
             }
         }
     }
 }
 private static byte[] Encode(Bitmap icon, int quality)
 {
     using (JpegEncoder jpegEncoder = new JpegEncoder())
     {
         BitmapData bitmapdata = icon.LockBits(new Rectangle(0, 0, icon.Width, icon.Height), ImageLockMode.ReadOnly, icon.PixelFormat);
         byte[]     numArray   = new byte[icon.Height * bitmapdata.Stride];
         Marshal.Copy(bitmapdata.Scan0, numArray, 0, numArray.Length);
         icon.UnlockBits(bitmapdata);
         IconConverter.CorrectRgbChannel(numArray, icon.PixelFormat);
         IconConverter.ThrowExceptionIfJpegResultFail(jpegEncoder.SetPixelData(numArray, icon.PixelFormat, IconConverter.ToSize(icon.PhysicalDimension), 1));
         jpegEncoder.SetQuality(quality);
         jpegEncoder.SetSamplingRatio(SamplingRatio.Ratio_444);
         byte[] outBuffer;
         IconConverter.ThrowExceptionIfJpegResultFail(jpegEncoder.Encode(out outBuffer, new Exif()
         {
             Software = "Nintendo AuthoringTool"
         }));
         return(outBuffer);
     }
 }