【发布时间】:2015-04-17 09:00:45
【问题描述】:
我正在尝试将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,此代码在我的情况下不起作用。我在这里找到了这个“解决方案”: similar question 1 similar question 2
但他们在我的情况下不起作用,我认为这是因为他们没有使用绑定。而且我没有任何想法来解决它...
对不起我的英语,我希望有人可以帮助我:)
【问题讨论】:
-
你的 base64 字符串不是在
image变量中吗?在这种情况下,您需要直接调用byte[] fileBytes = Convert.FromBase64String(image);另外,在读取之前不要忘记重置流的位置:ms.Position = 0(就在bitmapImage.SetSource之前) -
@KooKiz ,我添加了 ms.position = 0,我有一个新的错误。**找不到组件。 (HRESULT 例外:0x88982F50)** 你有什么想法吗?谢谢你的回答:)
标签: c# image windows-phone-8 binding base64