问题描述
我想知道如何将一个 ImageSource
对象(在我的情况下,图像元素的源属性)转换为 WriteableBitmap
。 WriteableBitmap(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);
}
}
返回缓冲区;
}
问题是 BitmapImage
不再具有 StreamSource
方法。
我在MSDN论坛上得到了答案。
I was wondering how I could go about converting an ImageSource
object (in my case the source property of an image element) to a WriteableBitmap
. The WriteableBitmap(BitmapSource)
constructor doesn't work in Windows 8 Metro, so I can't do that.
I've tried to load the ImageSource
object into a stream and then use WriteableBitmap.SetSource(stream)
, but I can't figure out how do load the image in a stream either.
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);
I've also added this helper method I found online:
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;
}
The problem is that BitmapImage
no longer has the StreamSource
method.
I got an answer on the MSDN forums.
这篇关于转换的ImageSource在Metro Windows中WriteableBitmap的8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!