public static void PatchAwayExif(Stream inStream, out MemoryStream exifStream)
 {
     exifStream = new MemoryStream();
     byte[] numArray = new byte[2] {
         (byte)inStream.ReadByte(), (byte)inStream.ReadByte()
     };
     if ((int)numArray[0] != (int)byte.MaxValue || (int)numArray[1] != 216)
     {
         return;
     }
     ImagePreprocessor.SkipAppHeaderSection(inStream, out exifStream);
 }
        public static void PreprocessImage(Stream stream, int desiredSizeInPx, bool closeStream, Action <ImagePreprocessResult> completionCallback)
        {
            Stopwatch.StartNew();
            try
            {
                MemoryStream exifStream;
                ImagePreprocessor.PatchAwayExif(stream, out exifStream);
                long position = stream.Position;
                bool keepExif = AppGlobalStateManager.Current.GlobalState.SaveLocationDataOnUpload;
                stream.Position = 0L;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(stream);
                WriteableBitmap wb     = new WriteableBitmap((BitmapSource)bitmapImage);
                ExifReader      reader = (ExifReader)null;
                stream.Position = 0L;
                reader          = new ExifReader(stream);
                bool flag = false;
                switch (reader.info.Orientation)
                {
                case ExifOrientation.BottomRight:
                    wb   = wb.Rotate(180);
                    flag = true;
                    break;

                case ExifOrientation.TopRight:
                    wb   = wb.Rotate(90);
                    flag = true;
                    break;

                case ExifOrientation.BottomLeft:
                    wb   = wb.Rotate(270);
                    flag = true;
                    break;
                }
                int pixelWidth  = wb.PixelWidth;
                int pixelHeight = wb.PixelHeight;
                int num1        = pixelWidth * pixelHeight;
                if (desiredSizeInPx > num1)
                {
                    desiredSizeInPx = num1;
                }
                if (num1 > desiredSizeInPx | flag)
                {
                    MemoryStream ms            = new MemoryStream();
                    double       num2          = Math.Sqrt((double)num1 / (double)desiredSizeInPx);
                    int          resizedWidth  = (int)Math.Round((double)pixelWidth / num2);
                    int          resizedHeight = (int)Math.Round((double)pixelHeight / num2);
                    ThreadPool.QueueUserWorkItem((WaitCallback)(o =>
                    {
                        try
                        {
                            Stream resultStream;
                            wb.SaveJpeg((Stream)ms, resizedWidth, resizedHeight, 0, VKConstants.JPEGQUALITY);
                            ms.Position = 0;
                            if (keepExif)
                            {
                                exifStream = new MemoryStream(ImagePreprocessor.ResetOrientation(reader.info.OrientationOffset, exifStream.ToArray(), reader.info.LittleEndian));
                                resultStream = ImagePreprocessor.MergeExif((Stream)ms, exifStream);
                                Logger.Instance.Info("RESIZED JPEG SIZE: " + (object)resultStream.Length);
                                ms.Close();
                            }
                            else
                            {
                                resultStream = ms;
                            }
                            if (closeStream)
                            {
                                stream.Close();
                            }
                            exifStream.Close();
                            resultStream.Position = 0L;
                            GC.Collect();
                            completionCallback(new ImagePreprocessResult((Stream)resultStream, resizedWidth, resizedHeight));
                        }
                        catch (Exception ex)
                        {
                            Logger.Instance.Error("Failed to resize image", ex);
                        }
                    }));
                }
                else
                {
                    MemoryStream resultStream = new MemoryStream();
                    if (keepExif)
                    {
                        stream.Position = 0;
                        stream.CopyTo((Stream)resultStream);
                        if (closeStream)
                        {
                            stream.Close();
                        }
                    }
                    else
                    {
                        stream.Position = 0;
                        resultStream.WriteByte((byte)stream.ReadByte());
                        resultStream.WriteByte((byte)stream.ReadByte());
                        stream.Position = position;
                        stream.CopyTo((Stream)resultStream);
                    }
                    if (closeStream)
                    {
                        stream.Close();
                    }
                    exifStream.Close();
                    resultStream.Position = 0;
                    GC.Collect();
                    completionCallback(new ImagePreprocessResult((Stream)resultStream, pixelWidth, pixelHeight));
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.Error("Failed to resize image", ex);
            }
        }