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;
		}
Exemplo n.º 2
0
        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);
        }