我正在尝试解码 Base64 图像并将其放入 WPF 图像源中。但是,我使用的代码有一个错误:
没有找到适合完成此操作的成像组件。
我已经使用在线 Base64 解码器仔细检查了我拥有的 Base64 字符串实际上是正确的 Base64 编码,所以我知道它不是这样。
My code:
byte[] binaryData = Convert.FromBase64String(desc.Icon_Path);
MemoryStream ms = new MemoryStream(binaryData, 0, binaryData.Length);
ms.Write(binaryData, 0, binaryData.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
icon.Source = ToWpfImage(image);
ms.Dispose();
public BitmapImage ToWpfImage(System.Drawing.Image img)
{
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage ix = new BitmapImage();
ix.BeginInit();
ix.CacheOption = BitmapCacheOption.OnLoad;
ix.StreamSource = ms;
ix.EndInit();
return ix;
}
我可能做错了什么?
鉴于 Base64 字符串包含可以由 WPF 之一解码的编码图像缓冲区位图解码器,您不需要比这更多的代码:
public static BitmapSource BitmapFromBase64(string b64string)
{
var bytes = Convert.FromBase64String(b64string);
using (var stream = new MemoryStream(bytes))
{
return BitmapFrame.Create(stream,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)