gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:03:51 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com