byte []到BitmapImage转换失败 [英] byte[] to BitmapImage conversion fails
问题描述
我有问题。我想将 BitmapImage
转换为 byte []
数组并返回。
I have a problem. I want to convert BitmapImage
into byte[]
array and back.
我写了这些方法:
public static byte[] ToByteArray(this BitmapImage bitmapImage)
{
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource.CopyTo(ms);
bitmapImage.EndInit();
bytes = ms.ToArray();
}
return bytes;
}
public static BitmapImage ToBitmapImage(this byte[] bytes, int width, int height)
{
BitmapImage bitmapImage = new BitmapImage();
using (MemoryStream ms = new MemoryStream(bytes))
{
ms.Position = 0;
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = ms;
bitmapImage.EndInit(); // HERE'S AN EXCEPTION!!!
}
return bitmapImage;
}
第一个工作正常,但当我尝试从<$ c $转换时c> byte [] 进入 BitmapImage
我得到了 NotSupportedException
...为什么?如何更正第二种方法的代码?
First one works fine, but when I try to convert from byte[]
into BitmapImage
I got a NotSupportedException
... Why? How to correct the code of the 2nd method?
推荐答案
你的 ToByteArray有两个问题
方法。
首先它调用 BeginInit
和 EndInit
在已经初始化的BitmapImage实例上。这是不允许的,请参阅例外列表。 aspxrel =nofollow> BeginInit 。
First it calls BeginInit
and EndInit
on an already initialized BitmapImage instance. This is not allowed, see the Exceptions list in BeginInit.
其次,无法在从Uri而不是Stream创建的BitmapImage上调用该方法。然后 StreamSource
属性将是 null
。
Second, the method could not be called on a BitmapImage that was created from an Uri instead of a Stream. Then the StreamSource
property would be null
.
I建议实现如下所示的方法。此实现适用于任何BitmapSource,而不仅适用于BitmapImages。您可以通过选择合适的 BitmapEncoder ,例如 JpegBitmapEncoder
而不是 PngBitmapEncoder
。
I suggest to implement the method like shown below. This implementation would work for any BitmapSource, not only BitmapImages. And you are able to control the image format by selecting an appropriate BitmapEncoder, e.g. JpegBitmapEncoder
instead of PngBitmapEncoder
.
public static byte[] ToByteArray(this BitmapSource bitmap)
{
var encoder = new PngBitmapEncoder(); // or any other encoder
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using (var ms = new MemoryStream())
{
encoder.Save(ms);
return ms.ToArray();
}
}
此 ToByteArray
方法总是可以通过 ToBitmapImage
方法转换回BitmapImage。
An image buffer returned by this ToByteArray
method can always be converted back to a BitmapImage by your ToBitmapImage
method.
请注意,您的 ToBitmapImage
方法的宽度和高度参数目前尚未使用。
And please note that the width and height arguments of your ToBitmapImage
method are currently unused.
UPDATE
解码方法的另一种实现可能如下所示,尽管它不返回BitmapImage而只返回基数的一个实例class BitmapSource。但是,您可以将返回类型更改为 BitmapFrame 。
An alternative implementation of the decoding method could look like shown below, although it does not return a BitmapImage but only an instance of the base class BitmapSource. You may however change the return type to BitmapFrame.
public static BitmapSource ToBitmapImage(this byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
return decoder.Frames[0];
}
}
这篇关于byte []到BitmapImage转换失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!