/// <summary> /// Update preview image. /// </summary> private async void UpdatePreviewImage() { double sourceImageWidthScale = this.imageCanvas.Width / this.sourceImagePixelWidth; double sourceImageHeightScale = this.imageCanvas.Height / this.sourceImagePixelHeight; Size previewImageSize = new Size( this.selectedRegion.SelectedRect.Width / sourceImageWidthScale, this.selectedRegion.SelectedRect.Height / sourceImageHeightScale); double previewImageScale = 1; if (previewImageSize.Width <= this.imageCanvas.Width && previewImageSize.Height <= this.imageCanvas.Height) { } else { previewImageScale = Math.Min(this.imageCanvas.Width / previewImageSize.Width, this.imageCanvas.Height / previewImageSize.Height); } this.CroppedImage = await CropBitmap.GetCroppedBitmapAsync( this.sourceImageFile, new Point(this.selectedRegion.SelectedRect.X / sourceImageWidthScale, this.selectedRegion.SelectedRect.Y / sourceImageHeightScale), previewImageSize, previewImageScale); }
/// <summary> /// Loads the image. /// </summary> /// <param name="imageFile">The image file.</param> /// <exception cref="System.ArgumentOutOfRangeException">imageFile;Image is too small.</exception> public async Task LoadImage(StorageFile imageFile) { //Debug.WriteLine("LoadImage"); using (IRandomAccessStream fileStream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { this.sourceImageFile = imageFile; BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); this.sourceImagePixelHeight = decoder.PixelHeight; this.sourceImagePixelWidth = decoder.PixelWidth; } if (this.sourceImagePixelHeight < 2 * CornerSize || this.sourceImagePixelWidth < 2 * CornerSize) { // Image too small. throw new ArgumentOutOfRangeException("imageFile", "Image is too small."); } else { double sourceImageScale = 1; if (this.sourceImagePixelHeight > this.layoutRoot.ActualHeight || this.sourceImagePixelWidth > this.layoutRoot.ActualWidth) { sourceImageScale = Math.Min(this.layoutRoot.ActualWidth / this.sourceImagePixelWidth, this.layoutRoot.ActualHeight / this.sourceImagePixelHeight); } else { } if (sourceImageScale == 0) { // Control is invisible, unable to scale the source image. return; // This would be a bit harsh: // throw new InvalidOperationException("ImageCropper is not visible."); } this.sourceImage.Source = await CropBitmap.GetCroppedBitmapAsync( this.sourceImageFile, new Point(0, 0), new Size(this.sourceImagePixelWidth, this.sourceImagePixelHeight), sourceImageScale); this.CroppedImage = null; } }
async System.Threading.Tasks.Task <int> CaptureAndSave(UIElement element, Image img) { if (element.Visibility != Windows.UI.Xaml.Visibility.Visible) { return(0); } // capture var bitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap(); await bitmap.RenderAsync(element); double x, y, w, h; w = AreaRectangle.Width; h = AreaRectangle.Height; if (Double.IsNaN(w) || Double.IsNaN(h)) { return(0); } x = Canvas.GetLeft(AreaRectangle); y = Canvas.GetTop(AreaRectangle); //Check& correct for min sizes if (w < MIN_OCR_SIZE) { double ww; ww = MaskRectangle.Width; if (ww - x < MIN_OCR_SIZE) { x = ww - MIN_OCR_SIZE; } w = 40; } if (w < MIN_OCR_SIZE) { double hh; hh = MaskRectangle.Height; if (hh - y < MIN_OCR_SIZE) { y = hh - MIN_OCR_SIZE; } h = 40; } IBuffer buff = await bitmap.GetPixelsAsync(); InMemoryRandomAccessStream memw = new InMemoryRandomAccessStream(); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, memw); byte[] bytes = buff.ToArray(); var dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi; //TODODEBUG //new MessageDialog(String.Format("Rectangle: {0},{1} - {2}, {3}", x, y, w, h) +"\n"+ // String.Format("Params: {0} - {1}, {2}", dpi, bitmap.PixelWidth, bitmap.PixelHeight) + "\n" + // String.Format("WebView: {0},{1} - {2}, {3}", Canvas.GetLeft(wbMain), Canvas.GetTop(wbMain), wbMain.ActualWidth, wbMain.ActualHeight) + "\n" + // String.Format("Area: {0},{1} - {2}, {3}", Canvas.GetLeft(AreaRectangle), Canvas.GetTop(AreaRectangle), AreaRectangle.ActualWidth, AreaRectangle.ActualHeight) // ).ShowAsync(); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, dpi, dpi, bytes); await encoder.FlushAsync(); IRandomAccessStream sss = memw; BitmapEncoder coder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, sss); //Fixfor win9=10 (it's drowing with some scaling) double kx = 1, ky = 1; kx = bitmap.PixelWidth / wbMain.ActualWidth; ky = bitmap.PixelHeight / wbMain.ActualHeight; x = x * kx; y = y * ky; w = w * kx; h = h * ky; BitmapDecoder dec = null; try { dec = await BitmapDecoder.CreateAsync(sss); } catch (Exception ex) { } croppedBitmap = await CropBitmap.GetCroppedBitmapAsync(dec, new Point(x, y), new Size(w, h), 1); img.Source = croppedBitmap; return(0); }