c# - 从字节数组创建 BitMapImage
问题描述
如何从字节数组创建位图图像对象。这是我的代码:
System.Windows.Media.Imaging.BitmapImage image = new
System.Windows.Media.Imaging.BitmapImage();
byte[] data = new byte[10] { 1, 0, 0, 1, 1, 1, 0, 0, 1, 0 };
using (var ms = new System.IO.MemoryStream(data))
{
image.BeginInit();
image.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
}
运行 EndInit() 命令时,出现以下异常。
No imaging component suitable to complete this operation was found.
我希望这些线条应该创建一个尺寸为 1x10 像素的图像,包含两种颜色。
我究竟做错了什么?异常是什么意思?
提前致谢!
解决方案
创建位图图像时,图像是根据其编码(BitmapImage 和 Encoding)从您的源加载的。C# 支持的位图 有很多很多不同的编码。
您看到的错误可能是因为 BitmapImage 类没有找到从您的字节数组到支持的编码的合适转换。(从编码中可以看出,许多是 4 或 8 的倍数,而 10 不是)。
我建议创建一个字节数组,其中包含您想要的结果的正确编码内容。例如,Rbg24格式的每个像素将具有三个字节的数据。
推荐阅读
- haskell - 为预先存在的类型自动生成“PersistEntity”
- php - woocommerce 从页面底部移动侧边栏
- ssl - 使用 Apache Camel 的 netty4-http 组件发出 Https 请求
- python - 减少 CNN 模型中的验证损失
- assembly - 如何在正常的 DLL 中找到“奇怪的”ROP 小工具?编译器永远不会这样做。(面向回报的编程)
- javascript - 如何替换正则表达式的最后一个匹配项?
- c# - 如何以一定的数据包大小和速度对我的 udp 服务器进行压力测试
- css - 为什么这个 flexbox 不能滚动?
- javascript - TypeError:在 Google App 脚本的对象中找不到函数 forEach
- r - 使用 nls() 函数生成 von Bertalanffy 增长模型时出现“奇异梯度”错误