c# - UIElement 屏幕截图 UWP - C#

标签 c# bitmap win-universal-app screenshot

我在网络上搜索但我没有找到任何解决方案来截取 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

关于c# - UIElement 屏幕截图 UWP - C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36040165/

相关文章:

c# - 回发后将要上传的文件分配给 fileUpload 控件

c# - java中的string.format等效

c# - 连续创建位图会导致内存泄漏

.net - UWP(通用 Windows 应用程序)中的 LoopingSelector?

c# - UDP网络与多个网络

bitmap - FFMpeg 将视频转换为 8 位 BMP 帧

java - 在SQLite数据库中存储位图

c# - 如何在 UWP 上隐藏 ListView 垂直滚动条?

windows - 如何在 UWP 中获取操作系统版本

c# - XAML 设计器未加载