コード例 #1
0
        /// <summary>
        /// Update preview image.
        /// </summary>
        async void UpdatePreviewImage()
        {
            double sourceImageWidthScale  = imageCanvas.Width / this.sourceImagePixelWidth;
            double sourceImageHeightScale = imageCanvas.Height / this.sourceImagePixelHeight;


            Size previewImageSize = new Size(
                this.selectedRegion.SelectedRect.Width / sourceImageWidthScale,
                this.selectedRegion.SelectedRect.Height / sourceImageHeightScale);

            double previewImageScale = 1;

            if (previewImageSize.Width <= imageCanvas.Width &&
                previewImageSize.Height <= imageCanvas.Height)
            {
                this.previewImage.Stretch = Windows.UI.Xaml.Media.Stretch.None;
            }
            else
            {
                this.previewImage.Stretch = Windows.UI.Xaml.Media.Stretch.Uniform;

                previewImageScale = Math.Min(imageCanvas.Width / previewImageSize.Width,
                                             imageCanvas.Height / previewImageSize.Height);
            }



            this.previewImage.Source = await CropBitmap.GetCroppedBitmapAsync(
                this.sourceImageFile,
                new Point(this.selectedRegion.SelectedRect.X / sourceImageWidthScale, this.selectedRegion.SelectedRect.Y / sourceImageHeightScale),
                previewImageSize,
                previewImageScale);
        }
コード例 #2
0
        /// <summary>
        /// Save the cropped image.
        /// </summary>
        async private void saveImageButton_Click(object sender, RoutedEventArgs e)
        {
            bool unsnapped = ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap());

            if (!unsnapped)
            {
                NotifyUser("Cannot unsnap the sample.");
                return;
            }

            double widthScale  = imageCanvas.Width / this.sourceImagePixelWidth;
            double heightScale = imageCanvas.Height / this.sourceImagePixelHeight;

            FileSavePicker savePicker = new FileSavePicker();

            savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

            savePicker.FileTypeChoices.Add("JPG file", new List <string>()
            {
                ".jpg"
            });
            savePicker.FileTypeChoices.Add("JPEG file", new List <string>()
            {
                ".jpeg"
            });
            savePicker.FileTypeChoices.Add("PNG file", new List <string>()
            {
                ".png"
            });
            savePicker.FileTypeChoices.Add("BMP file", new List <string>()
            {
                ".bmp"
            });


            savePicker.SuggestedFileName = string.Format("{0}_{1}x{2}{3}",
                                                         sourceImageFile.DisplayName,
                                                         (int)Math.Floor(this.selectedRegion.SelectedRect.Width / widthScale),
                                                         (int)Math.Floor(this.selectedRegion.SelectedRect.Height / heightScale),
                                                         sourceImageFile.FileType);
            StorageFile croppedImageFile = await savePicker.PickSaveFileAsync();

            if (croppedImageFile != null)
            {
                await CropBitmap.SaveCroppedBitmapAsync(
                    sourceImageFile,
                    croppedImageFile,
                    new Point(this.selectedRegion.SelectedRect.X / widthScale, this.selectedRegion.SelectedRect.Y / heightScale),
                    new Size(this.selectedRegion.SelectedRect.Width / widthScale, this.selectedRegion.SelectedRect.Height / heightScale));

                this.NotifyUser("The cropped bitmap is saved.");
            }
        }
コード例 #3
0
        /// <summary>
        /// Let user choose an image and load it.
        /// </summary>
        async private void openImageButton_Click(object sender, RoutedEventArgs e)
        {
            bool unsnapped = ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap());

            if (!unsnapped)
            {
                NotifyUser("Cannot unsnap the sample.");
                return;
            }


            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".bmp");
            openPicker.FileTypeFilter.Add(".png");
            StorageFile imgFile = await openPicker.PickSingleFileAsync();

            if (imgFile != null)
            {
                this.previewImage.Source         = null;
                this.sourceImage.Source          = null;
                this.imageCanvas.Visibility      = Windows.UI.Xaml.Visibility.Collapsed;
                this.originalImageInfoText.Text  = string.Empty;
                this.selectInfoInBitmapText.Text = string.Empty;
                this.saveImageButton.IsEnabled   = false;

                // Ensure the stream is disposed once the image is loaded
                using (IRandomAccessStream fileStream = await imgFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    this.sourceImageFile = imgFile;
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

                    this.sourceImagePixelHeight = decoder.PixelHeight;
                    this.sourceImagePixelWidth  = decoder.PixelWidth;
                }

                if (this.sourceImagePixelHeight < 2 * this.CornerSize ||
                    this.sourceImagePixelWidth < 2 * this.CornerSize)
                {
                    this.NotifyUser(string.Format("Please select an image which is larger than {0}*{0}",
                                                  2 * this.CornerSize));
                    return;
                }
                else
                {
                    double sourceImageScale = 1;

                    if (this.sourceImagePixelHeight < this.sourceImageGrid.ActualHeight &&
                        this.sourceImagePixelWidth < this.sourceImageGrid.ActualWidth)
                    {
                        this.sourceImage.Stretch = Windows.UI.Xaml.Media.Stretch.None;
                    }
                    else
                    {
                        sourceImageScale = Math.Min(this.sourceImageGrid.ActualWidth / this.sourceImagePixelWidth,
                                                    this.sourceImageGrid.ActualHeight / this.sourceImagePixelHeight);
                        this.sourceImage.Stretch = Windows.UI.Xaml.Media.Stretch.Uniform;
                    }

                    this.sourceImage.Source = await CropBitmap.GetCroppedBitmapAsync(
                        this.sourceImageFile,
                        new Point(0, 0),
                        new Size(this.sourceImagePixelWidth, this.sourceImagePixelHeight),
                        sourceImageScale);

                    this.originalImageInfoText.Text = string.Format("Original Image Size: {0}*{1} ",
                                                                    this.sourceImagePixelWidth, this.sourceImagePixelHeight);
                }
            }
        }