private void MainCamera_LiveViewUpdated(Camera sender, Stream img)
 {
     try
     {
         using (WrapStream s = new WrapStream(img))
         {
             img.Position = 0;
             BitmapImage EvfImage = new BitmapImage();
             EvfImage.BeginInit();
             EvfImage.StreamSource = s;
             EvfImage.CacheOption  = BitmapCacheOption.OnLoad;
             EvfImage.EndInit();
             EvfImage.Freeze();
             Application.Current.Dispatcher.BeginInvoke(SetImageAction, EvfImage);
         }
     }
     catch (Exception ex) { ReportError(ex.Message, false); }
 }
        private static void MainCamera_LiveViewUpdated(Camera sender, Stream img)
        {
            LiveViewUpdates++;

            if (LiveViewUpdates == 1 || LiveViewUpdates % 100 == 0)
            {
                // Log only every 100 updates
                LogMessage($"Live view updated #{LiveViewUpdates}");
            }

            try
            {
                using (WrapStream s = new WrapStream(img))
                {
                    img.Position = 0;
                    BitmapImage EvfImage = new BitmapImage();
                    EvfImage.BeginInit();
                    EvfImage.StreamSource = s;
                    EvfImage.CacheOption  = BitmapCacheOption.OnLoad;
                    EvfImage.EndInit();
                    EvfImage.Freeze();

                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create((BitmapImage)EvfImage));
                    TempPreviewBuffer = new MemoryStream();
                    encoder.Save(TempPreviewBuffer);
                    PreviewBuffer = TempPreviewBuffer;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                LiveViewWaiter.Set();
            }
        }