/// <summary>
        /// Loads an image from the file system and calls the ImageLoaded XAML control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void ImageLoadButton_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".png");
            openPicker.FileTypeFilter.Add(".gif");
            openPicker.FileTypeFilter.Add(".bmp");

            StorageFile file = await openPicker.PickSingleFileAsync();

            if (file != null)
            {
                var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);

                _LocalPersistentObject.bitmapProcessingImage = BitmapFactory.New((int)decoder.PixelWidth, (int)decoder.PixelHeight);
                _LocalPersistentObject.bitmapProcessingImage.SetSource(stream);

                _LocalPersistentObject.originalLoadedImage = _LocalPersistentObject.bitmapProcessingImage;
            }

            MainImageFrame.Navigate(typeof(ImageLoadedView), _LocalPersistentObject);
        }
        /// <summary>
        /// Cropping tool for the Image
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///
        private async void CropImageButton_Click(object sender, RoutedEventArgs e)
        {
            if (_LocalPersistentObject.bitmapProcessingImage == null)
            {
                return;
            }

            if (!_LocalPersistentObject.isCroppingImage)
            {
                MainImageFrame.Navigate(typeof(ImageCroppingView), _LocalPersistentObject);
                CropImageButton.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 245, 245, 220));    // Beige: 245, 245, 220
                CropImageButton.Foreground = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 0, 0));
            }
            else
            {
                // MainImageFrame.Navigate(typeof(ImageCropping), _LocalPersistentObject);
                CropImageButton.Foreground             = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 245, 245, 220)); // Beige: 245, 245, 220
                CropImageButton.Background             = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 0, 0));
                _LocalPersistentObject.isCroppingImage = false;

                ImageCroppingView imageCroppingRef = _LocalPersistentObject.ImageCropperInstance as ImageCroppingView;

                _LocalPersistentObject.bitmapProcessingImage = await imageCroppingRef.GetCropppedImageBitmap();

                MainImageFrame.Navigate(typeof(ImageLoadedView), _LocalPersistentObject, new SuppressNavigationTransitionInfo());
            }
        }
        private void EditImageButton_Click(object sender, RoutedEventArgs e)
        {
            if (_LocalPersistentObject.bitmapProcessingImage == null)
            {
                return;
            }

            MainImageFrame.Navigate(typeof(EditImageView), _LocalPersistentObject);
        }
        /// <summary>
        /// Reset the currently loaded image to its original instance.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        ///
        private void ResetImage_Click(object sender, RoutedEventArgs e)
        {
            if (_LocalPersistentObject.bitmapProcessingImage == null)
            {
                return;
            }

            _LocalPersistentObject.bitmapProcessingImage = _LocalPersistentObject.originalLoadedImage;
            MainImageFrame.Navigate(typeof(ImageLoadedView), _LocalPersistentObject, new SuppressNavigationTransitionInfo());
        }
        /// <summary>
        /// Use AForge to automatically detect the image borders, and crop it to size
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AutoFitImage_Click(object sender, RoutedEventArgs e)
        {
            if (_LocalPersistentObject.bitmapProcessingImage == null)
            {
                return;
            }

            WriteableBitmap bmp = _LocalPersistentObject.bitmapProcessingImage as WriteableBitmap;

            bmp = bmp.Resize(1024, 1024, WriteableBitmapExtensions.Interpolation.Bilinear);
            Bitmap image = AForge.Imaging.Image.Clone((Bitmap)bmp, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight   = 50;
            blobCounter.MinWidth    = 50;

            blobCounter.ProcessImage(image);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            // check for rectangles
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            foreach (var blob in blobs)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                List <IntPoint> cornerPoints;

                // use the shape checker to extract the corner points
                if (shapeChecker.IsQuadrilateral(edgePoints, out cornerPoints))
                {
                    // only do things if the corners form a rectangle
                    if (shapeChecker.CheckPolygonSubType(cornerPoints) == PolygonSubType.Square)
                    {
                        // here i use the graphics class to draw an overlay, but you
                        // could also just use the cornerPoints list to calculate your
                        // x, y, width, height values.
                        List <AForge.Point> Points = new List <AForge.Point>();

                        bmp = bmp.Crop(cornerPoints[0].X,
                                       cornerPoints[0].Y,
                                       cornerPoints[2].X,
                                       cornerPoints[2].Y);
                    }
                }
            }

            bmp = bmp.Resize(640, 640, WriteableBitmapExtensions.Interpolation.Bilinear);
            _LocalPersistentObject.bitmapProcessingImage = (WriteableBitmap)bmp;

            MainImageFrame.Navigate(typeof(ImageLoadedView), _LocalPersistentObject, new SuppressNavigationTransitionInfo());
        }
        /// <summary>
        /// Loaded automatically when this window is loaded
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            _LocalPersistentObject = e.Parameter as PersistentObjects;

            if (_LocalPersistentObject != null)
            {
                if (_LocalPersistentObject.originalLoadedImage != null)
                {
                    MainImageFrame.Navigate(typeof(ImageLoadedView), _LocalPersistentObject);
                }
            }
        }