gpt4 book ai didi

c# - 在 Metro Windows 8 中将 ImageSource 转换为 WriteableBitmap

转载 作者:太空狗 更新时间:2023-10-29 23:47:18 25 4
gpt4 key购买 nike

我想知道如何将 ImageSource 对象(在我的例子中是图像元素的 source 属性)转换为 WriteableBitmapWriteableBitmap(BitmapSource) 构造函数在 Windows 8 Metro 中不起作用,所以我不能那样做。

我试图将 ImageSource 对象加载到流中,然后使用 WriteableBitmap.SetSource(stream),但我不知道如何加载流中的图像。

InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
BitmapEncoder enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ras);

byte[] pixelArray = new byte[(uint)photoBox.Height * (uint)photoBox.Width * 4];

enc.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight,
(uint)photoBox.Width, (uint)photoBox.Height, 96, 96, pixelArray);

我还添加了我在网上找到的这个辅助方法:

public Byte[] BufferFromImage(BitmapImage imageSource)
{
Stream stream = imageSource.StreamSource;
Byte[] buffer = null;
if (stream != null && stream.Length > 0)
{
using (BinaryReader br = new BinaryReader(stream))
{
buffer = br.ReadBytes((Int32)stream.Length);
}
}

return buffer;
}

问题是 BitmapImage 不再有 StreamSource 方法。

最佳答案

WinRT XAML 平台在位图类中有一些不幸的限制。 WinRT XAML Toolkit有一个FromBitmapImage你可以看看的方法。它基本上采用原始源并加载基于该源的 WriteableBitmap。目前它仅限于应用程序安装位置的位图,但这应该很容易扩展到来自网络的文件(例如使用 WebFile 类下载图像)。您需要执行一些特定于应用程序的代码的唯一情况是,如果您使用 SetStream 加载 BitmapImage - 原始 Uri 将丢失,但在这种情况下 - 您也可以使用 WriteableBitmap 而不是 BitmapImage 和在 WriteableBitmap 之间复制像素/字节是可能的。

关于c# - 在 Metro Windows 8 中将 ImageSource 转换为 WriteableBitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11797915/

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