gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:04:17 25 4
gpt4 key购买 nike

我正在为 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/

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