【发布时间】:2012-08-03 14:40:41
【问题描述】:
我想知道如何将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);
}
}
return buffer;
}
问题是BitmapImage 不再有StreamSource 方法。
【问题讨论】:
-
哦 - 最好编辑它以删除 M 字... :-)
标签: c# windows-runtime windows-store-apps writeablebitmap imagesource