Waqas Idrees
Waqas Idrees

Reputation: 1461

How to Save Specific Area of View as Image in UWP?

I have done lot of R & D on Google that how can I save specific area of my view as image in Universal Windows Store Application. We can do it easily in WPF. But Bitmap and some libraries are not the part of Universal Platform.

I tried this library as well but unable to do. https://github.com/teichgraf/WriteableBitmapEx

Upvotes: 0

Views: 794

Answers (1)

Arnaud Develay
Arnaud Develay

Reputation: 3970

Have a look at this class: RenderTargetBitmap in Windows.UI.Xaml.Media.Imaging namespace. It exposes the following method.

public IAsyncAction RenderAsync(UIElement element, Int32 scaledWidth, Int32 scaledHeight)

Here is a snipped to convert a UIElement to SoftwareBitmap

RenderTargetBitmap renderTarget = new RenderTargetBitmap();
await renderTarget.RenderAsync(uiElement);

IBuffer pixelBuffer = await renderTarget.GetPixelsAsync();
SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, (int) uiElement.RenderSize.Width, (int) uiElement.RenderSize.Height, BitmapAlphaMode.Premultiplied);

You can then read this help page : Create, Edit and Save bitmap images for more information on SoftwareBitmap usage.

Upvotes: 3

Related Questions