我正在尝试将base64字符串转换为图像并绑定结果。
这是我的xaml:
<Image Source="{Binding image64}">
为了确保我的base64字符串是正确的,我做到了:
public BitmapImage image64
{
get
{
**//Convert my path img to Base64.**
byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(image);
string base64String = System.Convert.ToBase64String(bytes);
MessageBox.Show("Base 64 String :[" + base64String + "]");
//Convert my img base64 to img.
byte[] fileBytes = Convert.FromBase64String(base64String);
using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
ms.Write(fileBytes, 0, fileBytes.Length);
BitmapImage bitmapImage = new BitmapImage();
**bitmapImage.SetSource(ms);**
return bitmapImage;
}
}
}
由于setSource,此代码在我的情况下不起作用。我在这里找到了这个“解决方案”:相似的问题1 相似的问题2
但是它们在我的情况下不起作用,我认为这是因为它们未使用绑定。而且我不知道要修复它...
对不起我的英语,我希望有人能帮助我:)
我找到了这个解决方案,它运作完美。
byte[] filebytes = Convert.FromBase64String(image);
MemoryStream ms = new MemoryStream(filebytes, 0, filebytes.Length);
BitmapImage mimage = new BitmapImage();
mimage.SetSource(ms);
return mimage;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句