在SL應用程序,我可以使用:如何獲得應用程序的屏幕截圖
var bitmap = new WriteableBitmap(uiElementForViewControl, new TranslateTransform());
但UWP,我怎麼做同樣的事情?
在SL應用程序,我可以使用:如何獲得應用程序的屏幕截圖
var bitmap = new WriteableBitmap(uiElementForViewControl, new TranslateTransform());
但UWP,我怎麼做同樣的事情?
使用RenderTargetBitmap來渲染UIElement(例如您的頁面),類似於您的代碼片段使用WriteableBitmap,然後使用BitmapEncoder將RenderTargetBitmap的像素編碼爲jpg或png以保存。
見https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imaging.rendertargetbitmap.aspx和https://msdn.microsoft.com/en-us/library/windows/apps/mt244351.aspx
隨着Control.DrawToBitmap
你可以捕捉一個表單,這是你正在尋找的「應用程序」。
它不工作 – CraigTayor
這確實是不幸的。我自己相信很多年前曾經在Windows Forms應用程序中使用它。我搜索了這個函數,並且確實發現它並不適用於所有事情,並且有很多方法。然而,我將不會發布他們,因爲Rob有一個很好的答案來幫助OP。 –
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();
}
}
謝謝你,我明白了。 – CraigTayor