CraigTayor
CraigTayor

Reputation: 23

How to get app screen shot

In the SL App, I can use:

var bitmap = new WriteableBitmap(uiElementForViewControl, new TranslateTransform());

But in UWP, how do I do the same thing?

Upvotes: 0

Views: 1060

Answers (3)

CraigTayor
CraigTayor

Reputation: 23

    private async Task SaveVisualElementToFile(FrameworkElement element, StorageFile file)
    {
        var renderTargetBitmap = new RenderTargetBitmap();
        await renderTargetBitmap.RenderAsync(element);
        var pixels = await renderTargetBitmap.GetPixelsAsync();

        using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);
            encoder.SetPixelData(
                BitmapPixelFormat.Bgra8, 
                BitmapAlphaMode.Ignore, 
                (uint)renderTargetBitmap.PixelWidth,
                (uint)renderTargetBitmap.PixelHeight,
                DisplayInformation.GetForCurrentView().LogicalDpi,
                DisplayInformation.GetForCurrentView().LogicalDpi,
                pixels.ToArray());
            await encoder.FlushAsync();
        }
    }

Upvotes: 1

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21899

Use RenderTargetBitmap to render a UIElement (such as your page) similar to how your snippet uses WriteableBitmap, then use a BitmapEncoder to encode the RenderTargetBitmap's pixels to a jpg or png to save out.

See https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.rendertargetbitmap.aspx and https://msdn.microsoft.com/en-us/library/windows/apps/mt244351.aspx

Upvotes: 1

online Thomas
online Thomas

Reputation: 9381

With Control.DrawToBitmap you can capture a Form, which is the "app" you are looking for.

Upvotes: 0

Related Questions