编程语言
首页 > 编程语言> > UIElement屏幕截图UWP-C#

UIElement屏幕截图UWP-C#

作者:互联网

我在网上搜索,但没有找到任何解决方案来对UWP(通用Windows平台)中的UI元素(以位图为例)进行屏幕截图.

解决方法:

您可以使用Render XAML进行位图
一些示例代码在这里:

    // Render to an image at the current system scale and retrieve pixel contents
    RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(RenderedGrid);
    var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

    var savePicker = new FileSavePicker();
    savePicker.DefaultFileExtension = ".png";
    savePicker.FileTypeChoices.Add(".png", new List<string> { ".png" });
    savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    savePicker.SuggestedFileName = "snapshot.png";

    // Prompt the user to select a file
    var saveFile = await savePicker.PickSaveFileAsync();

    // Verify the user selected a file
    if (saveFile == null)
        return;

    // Encode the image to the selected file on disk
    using (var fileStream = await saveFile.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,
            pixelBuffer.ToArray());

        await encoder.FlushAsync();
    }

样例
https://code.msdn.microsoft.com/windowsapps/XAML-render-to-bitmap-dd4f549f

标签:win-universal-app,screenshot,bitmap,c
来源: https://codeday.me/bug/20191118/2031972.html