从C#中的xml文件反序列化图像 [英] Deserializat an image from xml file in c#

查看:122
本文介绍了从C#中的xml文件反序列化图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XmlSerializer deserializer = new XmlSerializer(typeof(question_list));
                
                TextReader reader = new StreamReader("x1.xml");
                object obj = deserializer.Deserialize(reader);
                question_list XmlData = (question_list)obj;
                reader.Close();
                t1.Text = XmlData.q_list[0].mytext;
/////////////////////////////////////////////////////////////
                img1.Source = XmlData.q_list[0].img;// my issue
/////////////////////////////////////////////////////////////
              button1.Content=  XmlData.q_list[0].option1;
              button2.Content = XmlData.q_list[0].option2;
              button3.Content = XmlData.q_list[0].option2;

推荐答案

我猜您想将其加载到WPF控件中. Source属性指向一个ImageSource,通常是一个URL,但是我认为您想加载实际的图像数据(在xml中编码为Base64).您需要使用ImageSource-恰好是BitmapImage.参见下文,了解如何加载它,只需将内存设置为所加载的任何内容即可(代码来自stackoveflow,但我不会在此处放置链接,因为答案可能会令人困惑)

I guess you want to load it in a WPF control. Source property points to a ImageSource, usually an URL, but I think you want to load the actual image data (encoded Base64 in xml). You need to use ImageSource - precisely BitmapImage. See below how you can load it, just set the memory to whatever you loaded (code is from stackoveflow, but I don''t put the link here because the answers might be confusing)

using(MemoryStream memory = new MemoryStream())
{
    bitmap.Save(memory, ImageFormat.Png);
    memory.Position = 0;
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = memory; <------
    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
    bitmapImage.EndInit();
}


这篇关于从C#中的xml文件反序列化图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆