c#操作图片存入xml和显示图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            //弹出一个选择文件的对话框
            OpenFileDialog openFileDialog1 = new OpenFileDialog();           
            DialogResult ok = openFileDialog1.ShowDialog();
            if (ok == DialogResult.OK)
            {
                try {
                    XmlDocument myXmlDoc = new XmlDocument();
                    //(bin目录下放置一个标准的空的xml文件,命名为doc.xml)
                    myXmlDoc.Load(Application.StartupPath + "\\doc.xml");
                    XmlElement elem = myXmlDoc.CreateElement("image");
                    //打开图片文件,利用该图片构造一个文件流
                    FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
                    //使用文件流构造一个二进制读取器
                    BinaryReader br = new BinaryReader(fs);
                    byte[] imageBuffer = new byte[br.BaseStream.Length];
                    br.Read(imageBuffer,0,Convert.ToInt32(br.BaseStream.Length));
                    string textString = System.Convert.ToBase64String(imageBuffer);
                    fs.Close();
                    br.Close();
                    XmlText text = myXmlDoc.CreateTextNode(textString);
                    myXmlDoc.DocumentElement.AppendChild(elem);
                    myXmlDoc.DocumentElement.LastChild.AppendChild(text);
                    myXmlDoc.Save(Application.StartupPath + "\\docSave.xml");
                    MessageBox.Show("读写结束!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //创建XmlDocument对象
                XmlDocument doc = new XmlDocument();
                //载入文件
                doc.Load(Application.StartupPath + "\\docSave.xml");               
                XmlNodeList nodeList = doc.GetElementsByTagName("image");
                //得到该节点
                XmlNode ImageNode = nodeList[0];
                //得到节点内的二进制代码
                string PicByte = ImageNode.InnerXml;
                //转换为byte[]
                byte[] b = Convert.FromBase64String(PicByte);
                System.IO.MemoryStream sm = new MemoryStream();
                //写入到流中
                sm.Write(b, 0, b.Length);
                pictureBox1.Image = Image.FromStream(sm);


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
           

        }

     
    }
}

 

转载于:https://my.oschina.net/u/3544533/blog/2236827

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中可以通过XmlDocument类来操作XML文件,其中XmlRoot、XmlAttribute和XmlElement是XML文件中的三种常见节点类型。 1. XmlRoot:表示XML文件的根节点,通过XmlRootAttribute可以给根节点指定名称和命名空间。 2. XmlAttribute:表示XML文件中的属性节点,通过XmlAttribute类可以获取或设置属性的值。 3. XmlElement:表示XML文件中的元素节点,通过XmlElement类可以获取或设置元素的值。 下面是一个使用XmlDocument类操作XML文件的示例: ``` // 加载XML文件 XmlDocument doc = new XmlDocument(); doc.Load("test.xml"); // 获取根节点 XmlNode root = doc.DocumentElement; // 获取指定元素节点 XmlNodeList nodes = root.SelectNodes("//book"); // 获取元素节点的属性值 foreach (XmlNode node in nodes) { XmlAttribute attr = node.Attributes["id"]; if (attr != null) { Console.WriteLine("Book ID: " + attr.Value); } } // 获取元素节点的值 XmlNode titleNode = nodes[0].SelectSingleNode("title"); if (titleNode != null) { Console.WriteLine("Book Title: " + titleNode.InnerText); } // 创建新的元素节点 XmlElement newElement = doc.CreateElement("book"); XmlAttribute newAttr = doc.CreateAttribute("id"); newAttr.Value = "3"; newElement.Attributes.Append(newAttr); newElement.InnerText = "C# Programming"; root.AppendChild(newElement); // 保存XML文件 doc.Save("test.xml"); ``` 上述代码中,我们通过XmlDocument类加载了一个名为test.xmlXML文件,并获取了根节点和指定的元素节点。然后,我们通过XmlAttribute和XmlElement类获取了元素节点的属性值和节点值,并创建了一个新的元素节点,并将其添加到根节点中。最后,我们通过Save方法将修改后的XML文件保存到磁盘中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值