C# UIElement屏幕截图UWP-C
我在web中搜索,但在UWP-通用Windows平台中,我没有找到任何解决方案,可以通过示例在位图中截图UI元素。您可以使用Render XAML绘制位图 这里有一些示例代码:C# UIElement屏幕截图UWP-C,c#,bitmap,win-universal-app,screenshot,C#,Bitmap,Win Universal App,Screenshot,我在web中搜索,但在UWP-通用Windows平台中,我没有找到任何解决方案,可以通过示例在位图中截图UI元素。您可以使用Render XAML绘制位图 这里有一些示例代码: // Render to an image at the current system scale and retrieve pixel contents RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); await
// 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();
}
示例
这是一位官员,您可以检查一下。虽然此示例适用于Windows 8.1应用商店应用程序,但在UWP中的实现是相同的。这不是您想要的:@JayZuo MSFT,谢谢。但是对象返回RenderTargetBitmap的类型,但我需要位图格式的此图像屏幕截图,因为我确实需要执行args.DragUI.SetContentFromBitmapImagemyBitmap;在拖动UI元素时。ThanksRenderTargetBItmap可以使用转换为位图BitmapEncoder@Sidewinder94谢谢简而言之,我无法从UI元素捕获图像并转换为BitmapImage?泰