[英]how to convert byte[] into BitmapFrame c#
I have tryed this but have exception - Operation is not valide due to current state of the object 我已经尝试过但是有例外-由于对象的当前状态,操作无效
private BitmapFrame backconvertor(byte[] incomingBuffer)
{
BitmapImage bmpImage = new BitmapImage();
MemoryStream mystream = new MemoryStream(incomingBuffer);
bmpImage.StreamSource = mystream;
BitmapFrame bf = BitmapFrame.Create(bmpImage);
return bf;
}
Error rising when I am trying to 当我尝试时错误上升
return backconvertor(buff);
in other function (buff - is ready!) 在其他功能中(buff-准备好了!)
Documentation indicates that in order to initialize the image, you need to do it between BeginInit
and EndInit
. 文档指出,为了初始化图像,您需要在BeginInit
和EndInit
之间进行EndInit
。 That is: 那是:
bmpImage.BeginInit();
bmpImage.StreamSource = mystream;
bmpImage.EndInit();
Or, you can pass the stream to the constructor: 或者,您可以将流传递给构造函数:
bmpImage = new BitmapImage(mystream);
See http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage.begininit.aspx for an example and more discussion of BeginInit
. 有关示例和BeginInit
更多讨论,请参见http://msdn.microsoft.com/zh-cn/library/system.windows.media.imaging.bitmapimage.begininit.aspx 。
This is what I have in a WPF Converter to handle byte to BitmapFrame and it works perfectly: 这是我在WPF转换器中用于处理字节到BitmapFrame的内容,它可以完美地工作:
var imgBytes = value as byte[];
if (imgBytes == null)
return null;
using (var stream = new MemoryStream(imgBytes))
{
return BitmapFrame.Create(stream,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
Also its thread safe as I have used it in Task.Run before also. 就像我之前在Task.Run中使用它一样,它的线程也是安全的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.