예제 #1
0
        public async Task <ImageSource> GetCropImageSource()
        {
            double sourceImageWidthScale  = imageCanvas.Width / this.sourceImagePixelWidth;
            double sourceImageHeightScale = imageCanvas.Height / this.sourceImagePixelHeight;


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

            double previewImageScale = 1;

            if (previewImageSize.Width <= imageCanvas.Width &&
                previewImageSize.Height <= imageCanvas.Height)
            {
            }
            else
            {
                previewImageScale = Math.Min(imageCanvas.Width / previewImageSize.Width,
                                             imageCanvas.Height / previewImageSize.Height);
            }

            return(await BitmapHelper.GetCroppedBitmapAsync(
                       this.SourceImageFile,
                       new Point(this.CropSelection.SelectedRect.X / sourceImageWidthScale, this.CropSelection.SelectedRect.Y / sourceImageHeightScale),
                       previewImageSize,
                       previewImageScale));
        }
        private async void OnTempImageFileChanged()
        {
            if (_isTemplateLoaded && SourceImageFile != null && TempImageFile != null)
            {
                scrollViewer.ZoomToFactor(1);
                // Ensure the stream is disposed once the image is loaded
                using (IRandomAccessStream fileStream = await TempImageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

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

                if (this.sourceImagePixelHeight < 2 * CropSelection.MinSelectRegionSize ||
                    this.sourceImagePixelWidth < 2 * CropSelection.MinSelectRegionSize)
                {
                    MessageDialog dialog = new MessageDialog("Image size is (" + sourceImagePixelWidth + "," + sourceImagePixelHeight + ") now and should be more than " + 2 * CropSelection.MinSelectRegionSize + " px");
                    await dialog.ShowAsync();
                }
                else
                {
                    double sourceImageScale = 1;

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

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

                    if (preSource != null)
                    {
                        WriteableBitmap pre    = preSource as WriteableBitmap;
                        var             source = this.sourceImage.Source as WriteableBitmap;
                        if (pre.PixelWidth == source.PixelWidth && pre.PixelHeight == source.PixelHeight)
                        {
                            this.editImage.Source = this.sourceImage.Source;
                        }
                    }
                }
            }
        }
예제 #3
0
        private async void OnSourceImageFileChanged(StorageFile newFile)
        {
            if (_isTemplateLoaded && newFile != null && this.ActualWidth > 0 && this.ActualHeight > 0)
            {
                await ImageHelper.StorageFileToStoragefileWithRightDirection(newFile);

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

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

                if (this.sourceImagePixelHeight < 2 * 30 ||
                    this.sourceImagePixelWidth < 2 * 30)
                {
                }
                else
                {
                    double sourceImageScale = 1;

                    if (this.sourceImagePixelHeight < this.ActualHeight &&
                        this.sourceImagePixelWidth < this.ActualWidth)
                    {
                        this.sourceImage.Stretch = Windows.UI.Xaml.Media.Stretch.None;
                    }
                    else
                    {
                        sourceImageScale = Math.Min(this.ActualWidth / this.sourceImagePixelWidth,
                                                    this.ActualHeight / this.sourceImagePixelHeight);
                        this.sourceImage.Stretch = Windows.UI.Xaml.Media.Stretch.Uniform;
                    }
                    this.sourceImage.ImageOpened += SourceImage_ImageOpened;
                    this.sourceImage.Source       = await BitmapHelper.GetCroppedBitmapAsync(
                        newFile,
                        new Point(0, 0),
                        new Size(this.sourceImagePixelWidth, this.sourceImagePixelHeight),
                        sourceImageScale);

                    await Task.Delay(100);

                    ReSetSelectionRect();
                }
            }
        }