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());
            }
           

        }

     
    }
}
相關文章
相關標籤/搜索