如何获取应用程序屏幕截图
How to get app screen shot
在SL App中,我可以使用:
var bitmap = new WriteableBitmap(uiElementForViewControl, new TranslateTransform());
但是在 UWP 中,我该如何做同样的事情?
使用 Control.DrawToBitmap
您可以捕获一个表单,这就是您正在寻找的 "app"。
使用 RenderTargetBitmap 渲染 UIElement(例如您的页面),类似于您的代码段使用 WriteableBitmap 的方式,然后使用 BitmapEncoder 将 RenderTargetBitmap 的像素编码为 jpg 或 png 以保存。
参见 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
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();
}
}
在SL App中,我可以使用:
var bitmap = new WriteableBitmap(uiElementForViewControl, new TranslateTransform());
但是在 UWP 中,我该如何做同样的事情?
使用 Control.DrawToBitmap
您可以捕获一个表单,这就是您正在寻找的 "app"。
使用 RenderTargetBitmap 渲染 UIElement(例如您的页面),类似于您的代码段使用 WriteableBitmap 的方式,然后使用 BitmapEncoder 将 RenderTargetBitmap 的像素编码为 jpg 或 png 以保存。
参见 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
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();
}
}