c# - 在 WinRT Metro 中将控件渲染到图像中

标签 c# windows-8 microsoft-metro rendering windows-runtime

我正在为 Windows 8 Metro 创建一个应用程序,我需要将框架控件渲染为图像并将其保存到硬盘,但不知道如何操作。我可以使用 SharpDX 来实现此目的吗?我能怎么做?预先感谢您的帮助

最佳答案

如果您不介意向 SharpDX 程序集添加依赖项,justin.m.chase 的方法当然有效。 RenderTargetBitmap 类可能非常适合您想要完成的任务。

// Render XAML to bitmap
var bitmap = new  RenderTargetBitmap();
await bitmap.RenderAsync(elementToRender);

// get the pixels
IBuffer pixelBuffer = await bitmap.GetPixelsAsync();
byte[] pixels = pixelBuffer.ToArray();

// write the pixels to a InMemoryRandomAccessStream
var stream = new  InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96, 96, pixels);

await encoder.FlushAsync();
stream.Seek(0);

关于c# - 在 WinRT Metro 中将控件渲染到图像中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12640855/

相关文章:

javascript - 如何在 Javascript Metro 应用程序中检测右键单击 ListView 中的项目

c# - 如何在 Metro 应用中调试共享合约?

c# - 如何在任何给定时间引发事件

c# - 连续页面 Crystal 报表查看器

c# - 提供 .NET 方法作为委托(delegate)回调

c# - 缺少 app.config 文件, "Add New Item"上的选项不显示配置文件

android - 将数据从 Web View 传递到 native 应用程序(Windows、iOS 和 Android)的最佳方法

javascript - 如何为 Windows 8 和 Android 平板电脑制作 D3js 应用程序的 Web 应用程序

c# - 名为 : 'Admin' was not found 的 AuthorizationPolicy

silverlight - Metro UI样式的Silverlight库