如何将 Byte[] 转换为 BitmapImage
我需要帮助,我有这个方法可以从 Byte[]
public BitmapSource ByteToBitmapSource(byte[] image)
{
BitmapImage imageSource = new BitmapImage();
using (MemoryStream stream = new MemoryStream(image))
{
stream.Seek(0, SeekOrigin.Begin);
imageSource.BeginInit();
imageSource.StreamSource = stream;
imageSource.CacheOption = BitmapCacheOption.OnLoad;
imageSource.EndInit();
}
return imageSource;
}
imageSource.EndInit();
获取 BitmapImage 抛出错误“我们发现没有适合完成此操作的成像组件。”
I need help, I have this method to get a BitmapImage from a Byte[]
public BitmapSource ByteToBitmapSource(byte[] image)
{
BitmapImage imageSource = new BitmapImage();
using (MemoryStream stream = new MemoryStream(image))
{
stream.Seek(0, SeekOrigin.Begin);
imageSource.BeginInit();
imageSource.StreamSource = stream;
imageSource.CacheOption = BitmapCacheOption.OnLoad;
imageSource.EndInit();
}
return imageSource;
}
imageSource.EndInit();
throws an error "We found no imaging component suitable to complete this operation."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
Image.Source
设置为 XAML 中的字节数组属性。如果您确实想要,可以在代码隐藏中执行此操作:
Set
Image.Source
to a byte array property in XAML.If you really want you can do this in code-behind:
您应该向我们提供有关您的图片的更多信息。
我可以假设系统不支持它,然后我建议您使用外部工具,例如 imageMagik< /a> 或任何其他特定于您的图像的转换器。
You should provide us with more info on your image.
I can assume it's unsupported by the system, which I would then advice you to use an external tool, such as imageMagik or any other converter specific to your image.
我做了类似的东西,但它不是用 BitmapImage 的,希望它能有所帮助...
首先,我从路径获取图像,所以我得到一个 BMP 并转换为 byte[]:
我在代码中这样称呼它 :
当我想将 byte[] 转换为 System.Windows.Controls.Image 时,我这样做
I've made something similar, but it's not with BitmapImage, hopes it can help...
First, I get the image from a path, so I get a BMP and cast into a byte[]:
I call this in my code like this:
And when I want to cast the byte[] into System.Windows.Controls.Image I do this:
这也可能有帮助:
This might also help: