void OnImageGrabbed(ManagedImage rawImage) { ManagedImage convertedBitmapSource = new ManagedImage(); rawImage.ConvertToBitmapSource(PixelFormatEnums.BGR8, convertedBitmapSource); try { if (Monitor.TryEnter(imageLock)) { try { BitmapSource bmpSource = convertedBitmapSource.bitmapsource; if (bmpSource == null) { return; } bmpSource.Freeze(); BitmapSource source = null; if (cameraType == CameraType.Top) { if ((App.Settings.CropHeight == 0 && App.Settings.CropWidth == 0) || (App.Settings.CropHeight + App.Settings.CropTop > bmpSource.Height) || (App.Settings.CropWidth + App.Settings.CropLeft > bmpSource.Width) ) { source = bmpSource; } else { source = new CroppedBitmap(bmpSource, new System.Windows.Int32Rect((int)App.Settings.CropLeft, (int)App.Settings.CropTop, (int)(App.Settings.CropWidth == 0 ? bmpSource.Width : App.Settings.CropWidth), (int)(App.Settings.CropHeight == 0 ? bmpSource.Height : App.Settings.CropHeight))); } } else { source = bmpSource; } source.Freeze(); latestBitmap = GetBitmap(source); //flip image vertically if (App.Settings.FlipImage) { latestBitmap.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY); } latestBitmapSrc = gColorFancy.Model.Utility.GetBitmapSource(latestBitmap); latestBitmapSrc.Freeze(); ImageReceivedEventNotification(latestBitmapSrc, latestBitmap, cameraType); } catch (Exception ex) { Debug.WriteLine("Error: exception: " + ex.Message); } finally { Monitor.Exit(imageLock); } } } catch (Exception ex) { lastError = ex.Message; } finally { convertedBitmapSource.Dispose(); GC.Collect(); } }