private async Task UpdateBackgroundFx(RenderTargetBitmap rtb)
        {
            ////await Task.Delay(1000);
            if (_renderedGrid.ActualHeight < 1 ||
                _backgroundFxImage == null)
            {
                return;
            }

            var pw = rtb.PixelWidth;
            var ph = rtb.PixelHeight;

            var wb = _backgroundFxImage.Source as WriteableBitmap;

            if (wb == null ||
                wb.PixelWidth != pw ||
                wb.PixelHeight != ph)
            {
                wb = new WriteableBitmap(pw, ph);
            }

            await ProcessBackgroundImage(rtb, wb, pw, ph);

            _backgroundFxImage.Source = wb;
        }
Beispiel #2
0
        public static async Task<IRandomAccessStream> RenderToRandomAccessStream(this UIElement element) {
            var rtb = new RenderTargetBitmap();
            await rtb.RenderAsync(element);

            IBuffer pixelBuffer = await rtb.GetPixelsAsync();
            byte[] pixels = pixelBuffer.ToArray();

            // Useful for rendering in the correct DPI
            DisplayInformation displayInformation = DisplayInformation.GetForCurrentView();

            var stream = new InMemoryRandomAccessStream();
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                 BitmapAlphaMode.Premultiplied,
                                 (uint)rtb.PixelWidth,
                                 (uint)rtb.PixelHeight,
                                 displayInformation.RawDpiX,
                                 displayInformation.RawDpiY,
                                 pixels);

            await encoder.FlushAsync();
            stream.Seek(0);

            return stream;
        }
        public static async Task<IBuffer> SaveAsPngIntoBufferAsync(this Canvas canvas, double _scaleFactor, int dpiForImage = 200)
        {
            //string currentresolution = Window.Current.Bounds.Width * scaleFactor + "*" + Window.Current.Bounds.Height * scaleFactor;
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(canvas);
            var pixels = await renderTargetBitmap.GetPixelsAsync();
            using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
            {
                var encoder = await
                    BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                byte[] bytes = pixels.ToArray();

                await CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                         BitmapAlphaMode.Ignore,
                                         (uint)(canvas.ActualWidth * _scaleFactor), (uint)(canvas.ActualHeight * _scaleFactor),
                                         dpiForImage, dpiForImage, bytes);
                });

                await encoder.FlushAsync();
                stream.Seek(0);
                var buffer = WindowsRuntimeBuffer.Create((int)stream.Size);
                await stream.ReadAsync(buffer, (uint)stream.Size, InputStreamOptions.None);
                return buffer;
            }
        }
Beispiel #4
0
        async void SaveTheForest()
        {
            var displayInformation = DisplayInformation.GetForCurrentView();
            var imageSize = new Size(ACTWIDTH, ACTHEIGHT);
            canvasOfAvaga.Measure(imageSize);
            canvasOfAvaga.UpdateLayout();
            canvasOfAvaga.Arrange(new Rect(0, 0, imageSize.Width, imageSize.Height));

            var renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(canvasOfAvaga, Convert.ToInt32(imageSize.Width), Convert.ToInt32(imageSize.Height));
            //await renderTargetBitmap.RenderAsync(canvasOfAvaga, Convert.ToInt32(ACTWIDTH), Convert.ToInt32(ACTHEIGHT));

            var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
            var picker = new FileSavePicker();
            picker.FileTypeChoices.Add(".jpg",new[] {".jpg"});
            var file = picker.PickSaveFileAsync();
            //var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("D:\\Screen.jpg", CreationCollisionOption.ReplaceExisting);
            using (var fileStream = await (await picker.PickSaveFileAsync()).OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, fileStream);

                encoder.SetPixelData(
                        BitmapPixelFormat.Bgra8,
                        BitmapAlphaMode.Ignore,
                        (uint)renderTargetBitmap.PixelWidth,
                        (uint)renderTargetBitmap.PixelHeight,
                        displayInformation.LogicalDpi,
                        displayInformation.LogicalDpi,
                        pixelBuffer.ToArray());

                await encoder.FlushAsync();
            }
        }
        // If showPicker == false => PicturesLibrary capability must be set in client app
        public static async Task<IStorageFile> CreateBitmapFromElement(FrameworkElement uielement, bool showPicker = true, string filenamePrefix = "receipt")
        {
            try
            {
                var renderTargetBitmap = new RenderTargetBitmap();
                await renderTargetBitmap.RenderAsync(uielement);

                var file = await SaveFile(showPicker, filenamePrefix);
                var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
                    encoder.SetPixelData(
                        BitmapPixelFormat.Bgra8,
                        BitmapAlphaMode.Ignore,
                        (uint) renderTargetBitmap.PixelWidth,
                        (uint) renderTargetBitmap.PixelHeight,
                        logicalDpi,
                        logicalDpi,
                        pixelBuffer.ToArray());

                    await encoder.FlushAsync();
                }
                return file;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return null;
            }
        }
Beispiel #6
0
        public static async Task<PixelColor[,]> GetPixels(RenderTargetBitmap source)
        {

            int width = source.PixelWidth;
            int height = source.PixelHeight;

            var result = new PixelColor[width, height];

            var buffer = await source.GetPixelsAsync();
            var bytes = buffer.ToArray();

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    var k = (y * width + x) * 4;
                    result[x, y] = new PixelColor
                    {
                        Blue = bytes[k],
                        Green = bytes[k + 1],
                        Red = bytes[k + 2]
                    };
                }
            }

            return result;
        }
Beispiel #7
0
        private async Task RenderUIElement(FrameworkElement elm, string fileName, int width, int height) {
            
            await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(1));

            using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                var renderTargetBitmap = new RenderTargetBitmap();
                await renderTargetBitmap.RenderAsync(elm);

                var pixels = await renderTargetBitmap.GetPixelsAsync();

                var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ms);
                encoder.SetPixelData(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Ignore,
                    (uint)renderTargetBitmap.PixelWidth,
                    (uint)renderTargetBitmap.PixelHeight,
                    logicalDpi,
                    logicalDpi,
                    pixels.ToArray());

                await encoder.FlushAsync();

                await X.Services.Image.Service.Instance.GenerateResizedImageAsync(width, elm.ActualWidth, elm.ActualHeight, ms, fileName + ".png", Services.Image.Service.location.TileFolder, height);
            }

        }
Beispiel #8
0
        async Task _render()
        {
            var r = new RenderTargetBitmap();

            await r.RenderAsync(OLEDRenderer);

            var colors = await PixelRender.GetPixels(r);

            for (var i = 0; i < r.PixelHeight; i++)
            {
                for (var x = 0; x < r.PixelWidth; x++)
                {
                    var pixel = colors[x, i];
                    var average = (pixel.Red + pixel.Green + pixel.Blue) / 3;
                    if (average > 0)
                    {
                        _oled.DrawPixel((ushort)x, (ushort)i, 1);
                    }
                    else
                    {
                        _oled.DrawPixel((ushort)x, (ushort)i, 0);
                    }
                }
            }
            _oled.Display();
            await Task.Yield();
        }
Beispiel #9
0
        public async Task<string> RenderToBase64(UIElement element)
        {
            RenderTargetBitmap rtb = new RenderTargetBitmap();
            await rtb.RenderAsync(element);

            var pixelBuffer = await rtb.GetPixelsAsync();
            var pixels = pixelBuffer.ToArray();

            // Useful for rendering in the correct DPI
            var displayInformation = DisplayInformation.GetForCurrentView();

            var stream = new InMemoryRandomAccessStream();
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                 BitmapAlphaMode.Premultiplied,
                                 (uint)rtb.PixelWidth,
                                 (uint)rtb.PixelHeight,
                                 displayInformation.RawDpiX,
                                 displayInformation.RawDpiY,
                                 pixels);

            await encoder.FlushAsync();
            stream.Seek(0);

            // read bytes
            var bytes = new byte[stream.Size];
            await stream.AsStream().ReadAsync(bytes, 0, bytes.Length);

            // create base64
            return Convert.ToBase64String(bytes);
        }
 /// <summary>
 /// Start of the Drag and Drop operation: we set some content and change the DragUI
 /// depending on the selected options
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="args"></param>
 private async void SourceGrid_DragStarting(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.DragStartingEventArgs args)
 {
     args.Data.SetText(SourceTextBox.Text);
     if ((bool)DataPackageRB.IsChecked)
     {
         // Standard icon will be used as the DragUIContent
         args.DragUI.SetContentFromDataPackage();
     }
     else if ((bool)CustomContentRB.IsChecked)
     {
         // Generate a bitmap with only the TextBox
         // We need to take the deferral as the rendering won't be completed synchronously
         var deferral = args.GetDeferral();
         var rtb = new RenderTargetBitmap();
         await rtb.RenderAsync(SourceTextBox);
         var buffer = await rtb.GetPixelsAsync();
         var bitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer,
             BitmapPixelFormat.Bgra8,
             rtb.PixelWidth,
             rtb.PixelHeight,
             BitmapAlphaMode.Premultiplied);
         args.DragUI.SetContentFromSoftwareBitmap(bitmap);
         deferral.Complete();
     }
     // else just show the dragged UIElement
 }
Beispiel #11
0
 private async void AppBarButton_Click(object sender, RoutedEventArgs e)
 {
     var b = new RenderTargetBitmap();
     await b.RenderAsync(WatchHubSection);
     var vm = this.DataContext as MainPageViewModel;
     vm.PurchaseCommand.Execute(b);
 }
        //Creates RenderTargetBitmap from UI Element
        async Task<RenderTargetBitmap> CaptureToStreamAsync(FrameworkElement uielement, IRandomAccessStream stream, Guid encoderId)
        {
            try
            {
                var renderTargetBitmap = new RenderTargetBitmap();
                await renderTargetBitmap.RenderAsync(uielement);
                var pixels = await renderTargetBitmap.GetPixelsAsync();
                var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
                var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
                encoder.SetPixelData(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Ignore,
                    (uint)renderTargetBitmap.PixelWidth,
                    (uint)renderTargetBitmap.PixelHeight,
                    logicalDpi,
                    logicalDpi,
                    /*pixels.ToArray()*/ null);

                await encoder.FlushAsync();
                return renderTargetBitmap;

            }
            catch
            {

            }

            return null;
        }
 public async void takeScreenShot() 
 {
     RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
     await renderTargetBitmap.RenderAsync(s);
     DataTransferManager.ShowShareUI();
     DataTransferManager.GetForCurrentView().DataRequested += OnShareDataRequested;
 }
		public static async Task RenderHelper(WriteableBitmap targetBitmap, FrameworkElement control, int x, int y)
		{
			RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
			const int PixelSize = 4;

			Frame topFrame;
			Page topPage;
			Panel topPanel;
			if (((topFrame = Window.Current.Content as Frame).Content != null)
				&& ((topPage = topFrame.Content as Page) != null)
				&& ((topPanel = topPage.Content as Panel) != null))
			{
				/// Create a temporary container for the control to be rendered
				var tmpPanel = new StackPanel()
				{
					// We will try to render the control offscreen to avoid flickering
					RenderTransform = new TranslateTransform()
					{
						X = 1000,
						Y = 1000
					}
				};
				var oldParent = control.Parent;
				try
				{
					tmpPanel.Children.Add(control);
					topPanel.Children.Add(tmpPanel);
					await renderTargetBitmap.RenderAsync(control);
				}
				finally
				{
					tmpPanel.Children.Remove(control);
					topPanel.Children.Remove(tmpPanel);
				}

				var array = (await renderTargetBitmap.GetPixelsAsync()).ToArray();

				using (var targetStream = targetBitmap.PixelBuffer.AsStream())
				{
					var startOfLine = y * targetBitmap.PixelWidth * PixelSize;
					for (int i = 0; i < renderTargetBitmap.PixelHeight
									&& i < targetBitmap.PixelHeight
									&& startOfLine < targetBitmap.PixelBuffer.Capacity; i++)
					{
						targetStream.Seek(startOfLine + (PixelSize * x), SeekOrigin.Begin);
						startOfLine = startOfLine + targetBitmap.PixelWidth * PixelSize;

						if (i * renderTargetBitmap.PixelWidth * PixelSize < targetBitmap.PixelBuffer.Capacity)
						{
							await targetStream.WriteAsync(array, i * renderTargetBitmap.PixelWidth * PixelSize, (int)Math.Min(Math.Min(renderTargetBitmap.PixelWidth, targetBitmap.PixelWidth - x) * PixelSize, (long)targetBitmap.PixelBuffer.Capacity));
						}
						else
						{
							break;
						}
					}
				}
			}
		}
 private static async Task RenderElementAsync(FrameworkElement element, IRandomAccessStream stream, Guid encoderId, Size size)
 {
     var bitmap = new RenderTargetBitmap();
     await bitmap.RenderAsync(element, (int)size.Width, (int)size.Height);
     var pixels = await bitmap.GetPixelsAsync();
     var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
     var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
     encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, logicalDpi, logicalDpi, pixels.ToArray());
     await encoder.FlushAsync();
 }
        /// <summary>
        /// Applys a blur to a UI element
        /// </summary>
        /// <param name="sourceElement">UIElement to blur, generally an Image control, but can be anything</param>
        /// <param name="blurAmount">Level of blur to apply</param>
        /// <returns>Blurred UIElement as BitmapImage</returns>
        public static async Task<BitmapImage> BlurElementAsync(this UIElement sourceElement, float blurAmount = 2.0f)
        {
            if (sourceElement == null)
                return null;

            var rtb = new RenderTargetBitmap();
            await rtb.RenderAsync(sourceElement);

            var buffer = await rtb.GetPixelsAsync();
            var array = buffer.ToArray();

            var displayInformation = DisplayInformation.GetForCurrentView();

            using (var stream = new InMemoryRandomAccessStream())
            {
                var pngEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                pngEncoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                     BitmapAlphaMode.Premultiplied,
                                     (uint) rtb.PixelWidth,
                                     (uint) rtb.PixelHeight,
                                     displayInformation.RawDpiX,
                                     displayInformation.RawDpiY,
                                     array);

                await pngEncoder.FlushAsync();
                stream.Seek(0);

                var canvasDevice = new CanvasDevice();
                var bitmap = await CanvasBitmap.LoadAsync(canvasDevice, stream);

                var renderer = new CanvasRenderTarget(canvasDevice,
                                                      bitmap.SizeInPixels.Width,
                                                      bitmap.SizeInPixels.Height,
                                                      bitmap.Dpi);

                using (var ds = renderer.CreateDrawingSession())
                {
                    var blur = new GaussianBlurEffect
                    {
                        BlurAmount = blurAmount,
                        Source = bitmap
                    };
                    ds.DrawImage(blur);
                }

                stream.Seek(0);
                await renderer.SaveAsync(stream, CanvasBitmapFileFormat.Png);

                var image = new BitmapImage();
                await image.SetSourceAsync(stream);

                return image;
            }
        }
Beispiel #17
0
 static async Task<RenderTargetBitmap> CaptureToStreamAsync(FrameworkElement uielement, IRandomAccessStream stream, Guid encoderId)
 {
     var renderTargetBitmap = new RenderTargetBitmap();
     await renderTargetBitmap.RenderAsync(uielement);
     IBuffer pixels = await renderTargetBitmap.GetPixelsAsync();
     double logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
     var encoder = await BitmapEncoder.CreateAsync(encoderId, stream);
     encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (UInt32)(renderTargetBitmap.PixelWidth), (UInt32)(renderTargetBitmap.PixelHeight), logicalDpi, logicalDpi, pixels.ToArray());
     await encoder.FlushAsync();
     return renderTargetBitmap;
 }
Beispiel #18
0
        /// <summary>
        /// Esegue il rendering di un FrameworkElement
        /// </summary>
        public async Task<WriteableBitmap> Screenshot(FrameworkElement ele)
        {
            // Render some UI to a RenderTargetBitmap
            var renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(ele);

            // Get the pixel buffer and copy it into a WriteableBitmap
            var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();
            var width = renderTargetBitmap.PixelWidth;
            var height = renderTargetBitmap.PixelHeight;
            var wbmp = await new WriteableBitmap(1, 1).FromPixelBuffer(pixelBuffer, width, height);

            return wbmp;
        }
        private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            var control = new LandscapeWeekView() { Width = 300, Height = 300 };

            TestGrid.Children.Add(control);

            var bitmap = new RenderTargetBitmap();
            await bitmap.RenderAsync(control);


            TestGrid.Children.Remove(control);

            TestGrid.Background = new ImageBrush() { ImageSource = bitmap };
        }
Beispiel #20
0
		private void func(string xamlTemplate, string hotel)
		{
			Canvas oCanvas = (Canvas)XamlReader.Parse(InjectData(xamlTemplate, hotel));   
			int  iWidth  =  (int)oCanvas.Width;   
			int  iHeight  =  (int)oCanvas.Height;   
			
			oCanvas.Arrange(new  Rect(0,  0,  iWidth,  iHeight));   
			
			RenderTargetBitmap  oRenderTargetBitmap  =  new  RenderTargetBitmap(iWidth,  iHeight,  96,  96, PixelFormats.Default);   
			
			oRenderTargetBitmap.Render(oCanvas);  
			
			// Save image using JpegBitmapEncoder 
		}
        private async Task UpdateFx()
        {
            if (_renderedGrid.ActualHeight < 1 ||
                _backgroundFxImage == null ||
                _foregroundFxImage == null)
            {
                return;
            }

            var rtb = new RenderTargetBitmap();
            await rtb.RenderAsync(_renderedGrid);

            await this.UpdateBackgroundFx(rtb);
            await this.UpdateForegroundFx(rtb);
        }
Beispiel #22
0
 private async void BuildButton_OnClick(object sender, RoutedEventArgs e)
 {
     var bitmap = new RenderTargetBitmap();
     await bitmap.RenderAsync(this.ContentGrid);
     var pixelBuffer = await bitmap.GetPixelsAsync();
     var name = DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + ".jpg";
     var file = await KnownFolders.SavedPictures.CreateFileAsync(name, CreationCollisionOption.GenerateUniqueName);
     using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
     {
         var dpi = DisplayInformation.GetForCurrentView().LogicalDpi;
         var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
         encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
             (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, dpi, dpi,
             pixelBuffer.ToArray());
         await encoder.FlushAsync();
     }
 }
Beispiel #23
0
        private async void OnTapped(object sender, TappedRoutedEventArgs e)
        {
            Type tp = sender.GetType();
            runSender.Text = tp.FullName;

            Type tpOrg = e.OriginalSource.GetType();
            runOrg.Text = tpOrg.FullName;

            UIElement u = e.OriginalSource as UIElement;

            if(u != null)
            {
                RenderTargetBitmap rtbmp = new RenderTargetBitmap();
                await rtbmp.RenderAsync(u);
                img.Source = rtbmp;
            }
        }
        public ColorDialog()
        {
            this.InitializeComponent();
            ImageBrush imageBrush = new ImageBrush();
            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
            Rectangle rectangle = new Rectangle();
            rectangle.Width = 100;
            rectangle.Height = 100;
            //colorDialogCanvas.Children.Add(rectangle);
            Image img = new Image();
            fileOpenPicker.FileTypeFilter.Add(".png");
            fileOpenPicker.FileTypeFilter.Add(".jpg");
            open();


        }
        /// <summary>
        ///   Renders the ui element into a bitmap frame using the specified scale.
        /// </summary>
        /// <param name = "element">The UI element.</param>
        /// <param name = "scale">The scale (default: 1).</param>
        /// <returns>The created bitmap frame</returns>
        public static BitmapSource RenderToBitmap(this UIElement element, double scale)
        {
            var renderWidth = (int) (element.RenderSize.Width * scale);
            var renderHeight = (int) (element.RenderSize.Height * scale);

            var renderTarget = new RenderTargetBitmap(renderWidth, renderHeight, 96, 96, PixelFormats.Pbgra32);
            var sourceBrush = new VisualBrush(element);

            var drawingVisual = new DrawingVisual();

            using (var drawingContext = drawingVisual.RenderOpen())
            {
                drawingContext.PushTransform(new ScaleTransform(scale, scale));
                drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(element.RenderSize.Width, element.RenderSize.Height)));
            }
            renderTarget.Render(drawingVisual);

            return renderTarget;
        }
        /// <summary>
        /// Processes the RenderTargetBitmap and outputs the result to the output WriteableBitmap.
        /// </summary>
        /// <param name="rtb">The RenderTargetBitmap that typically includes a screen grab of the portion of UI.</param>
        /// <param name="wb">The WriteableBitmap that the effect output is written to.</param>
        /// <param name="pw">The pixel width of both bitmaps.</param>
        /// <param name="ph">The pixel height of both bitmaps.</param>
        /// <returns>A task that completes when the processing is complete.</returns>
        public override async Task ProcessBitmapAsync(RenderTargetBitmap rtb, WriteableBitmap wb, int pw, int ph)
        {
            //var sw = new Stopwatch();
            //sw.Start();

            var rtbBuffer = await rtb.GetPixelsAsync();
            var rtbPixels = rtbBuffer.GetPixels();
            var wbBuffer = wb.PixelBuffer;
            var wbPixels = wbBuffer.GetPixels();

            // Blur
            int radius = 1;

            for (int x = 0; x < pw; x++)
                for (int y = 0; y < ph; y++)
                {
                    int x1min = Math.Max(0, x - radius);
                    int x1max = Math.Min(x + radius, pw - 1);
                    int y1min = Math.Max(0, y - radius);
                    int y1max = Math.Min(y + radius, ph - 1);

                    int count = (x1max - x1min + 1) * (y1max - y1min + 1) + 7;
                    var sum = new int[4];

                    for (int x1 = x1min; x1 <= x1max; x1++)
                        for (int y1 = y1min; y1 <= y1max; y1++)
                            for (int i = 0; i < 4; i++)
                                sum[i] +=
                                    (x == x1 && y == y1) ?
                                    rtbPixels.Bytes[4 * (y1 * pw + x1) + i] * 8 :
                                    rtbPixels.Bytes[4 * (y1 * pw + x1) + i];

                    for (int i = 0; i < 4; i++)
                        wbPixels.Bytes[4 * (y * pw + x) + i] = (byte)(sum[i] / count);
                }

            wbPixels.UpdateFromBytes();
            wb.Invalidate();

            //sw.Stop();
            //new MessageDialog(sw.ElapsedMilliseconds.ToString()).ShowAsync();
        }
        public static  async void templateCreator(Template t,Image I,Grid VisualTreeParent,int Hoffset, int Voffset,int ScaleFactor)
        {
            //say cheese
            VisualTreeParent.Children.Add(t);
            RenderTargetBitmap render = new RenderTargetBitmap();
            await render.RenderAsync(t);
            var result = await render.GetPixelsAsync();
            //print the photo
            WriteableBitmap source = new WriteableBitmap(ScaleFactor * render.PixelWidth, ScaleFactor * render.PixelHeight);
            using (Stream stream = source.PixelBuffer.AsStream())
            {
                Byte[] res = new Byte[source.PixelHeight * source.PixelWidth * 4];
                for (int i = 0; i < res.Length; i++)
                {
                    res[i] = 0xff;
                }
                //عيب يا ابو عيسي :P
                Byte[] kareem = result.ToArray();
                for (int i = 0, j = 0; i + 4 < source.PixelWidth * render.PixelHeight * 4 + Voffset * source.PixelWidth * 4; i = ((j / (render.PixelWidth * 4)) + Voffset) * source.PixelWidth * 4 + i % (render.PixelWidth * 4))
                {
                    if (kareem[j + 1] == 0 && kareem[j] == 0 && kareem[j + 2] == 0 && kareem[j + 3] == 255)
                    {
                        for (int l = 0; l < 3; l++)
                        {
                            res[i++ + Hoffset * 4] = kareem[j++];
                        }
                        res[i++ + Hoffset * 4] = 0;
                        j++;
                    }

                    else
                        for (int l = 0; l < 4; l++)
                        {
                            res[i++ + Hoffset * 4] = 0xff;
                            j++;
                        }
                }
                await stream.WriteAsync(res, 0, res.Length);
            }
            VisualTreeParent.Children.Remove(t);
            I.Source = source;
        }
Beispiel #28
0
        private async void OnPressed(object o, PointerRoutedEventArgs args)
        {
            if (args.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Pen)
                return;

            _qbs.Clear();
            foreach (var q in _mainpage.MainCanvas.Children.Where(c => c is QueryBoxView))
            {
                _qbs.Add((QueryBoxView)q);
            }

            if (!_inqCanvasPressed && args.OriginalSource is InqCanvas)
            {
                _inqCanvasPressed = true;
            }

            var pressedItem = (FrameworkElement)args.OriginalSource;
            if (_inqCanvasPressed && pressedItem.DataContext is QueryBoxListItemViewModel)
            {
                _pressedItem = pressedItem;
                _mainpage.PointerMoved += OnManipulationDelta;
                _pressedPointerId = args.Pointer.PointerId;
                foreach (var queryBoxView in _qbs)
                {
                    queryBoxView.IsBlocked = true;
                }

                var vm = (QueryBoxListItemViewModel)_pressedItem.DataContext;
                _graph = Controller.Instance.CreateLineGraph(new List<TimeSeries> {vm.TimeSeriesModel});
                _mainpage.MainCanvas.Children.Add(_graph);
                
                RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                await renderTargetBitmap.RenderAsync(_graph, (int)_graph.ActualWidth, (int)_graph.ActualHeight);
                // _draggedItem.Background = new SolidColorBrush(Colors.Black);
                if (!_mainpage.MainCanvas.Children.Contains(_draggedItem))
                    _mainpage.MainCanvas.Children.Add(_draggedItem);

                _draggedItem.Opacity = 0.5;
                _draggedItem.Source = renderTargetBitmap;

            }
        }
        public static async Task<string> CreateNormalTileImage(string cover, UIElement tile)
        {
            var fileName = MD5.GetMd5String(cover);
            var filePath = Path.Combine(LiveTilePicsDirectoryName, fileName);

            if (FileExists(filePath)) return filePath;
            if (cover.Contains("http"))
            {
                var target = new RenderTargetBitmap();
                tile.Opacity = 1;
                await target.RenderAsync(tile);
                tile.Opacity = 0;
                var pixels = await target.GetPixelsAsync();
                
            }
            else
            {
            }

            return filePath;
        }
Beispiel #30
0
        public async Task CaptureThumbnail(InMemoryRandomAccessStream ms)
        {
            var renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(_renderElement);

            var pixels = await renderTargetBitmap.GetPixelsAsync();

            var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ms);
            encoder.SetPixelData(
                BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Ignore,
                (uint)renderTargetBitmap.PixelWidth,
                (uint)renderTargetBitmap.PixelHeight,
                logicalDpi,
                logicalDpi,
                pixels.ToArray());

            await encoder.FlushAsync();

        }