XDocument xdoc = XDocument.Parse(rr.Nodes[1].ToString(), LoadOptions.None);
xdoc.Declaration = new XDeclaration("1.0", "UTF - 8", "yes");
var q = from b in xdoc.Descendants("PictureNo")
select b.Value;
foreach (string item in q.ToList())
textBox2.Text = item;
if (textBox2.Text == String.Empty)
{
Byte[] data2 = new Byte[0];
data2 = (Byte[])(Convert.FromBase64String(textBox2.Text));
string base64String1 = Convert.ToBase64String(data2, 0, data2.Length);
image.Source = "data:image/jpeg;base64," + base64String1;
答案 0 :(得分:1)
您可以使用以下方法将Base64
字符串转换为BitmapImage
,然后将其设置为Image.Source
。
public async Task<BitmapImage> Base64ToBitmapAsync(string source)
{
var byteArray = Convert.FromBase64String(source);
BitmapImage bitmap = new BitmapImage();
using (MemoryStream stream = new MemoryStream(byteArray))
{
await bitmap.SetSourceAsync(stream.AsRandomAccessStream());
}
return bitmap;
}
首先从Base64
源创建一个字节数组,然后将其用作MemoryStream
,您将其作为BitmapImage
的来源加载。
然后你只需:
image.Source = await Base64ToBitmapAsync( Convert.FromBase64String(textBox2.Text) );