欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net 將一個圖片以二進制值的形式存入Xml文件中的實例代碼

 更新時間:2013年07月24日 10:48:50   作者:  
這篇文章介紹了將一個圖片以二進制值的形式存入Xml文件中的實例代碼,有需要的朋友可以參考一下
復(fù)制代碼 代碼如下:

try
   {
    int readByte = 0;        //
    int bytesToRead = 100;       //數(shù)據(jù)緩沖區(qū)大小
    string fileName = "../../WriteXml.xml";   //要打開的文件
    //   this.textBox1.Text = string.Empty;           
    // 打開圖片文件,利用該圖片構(gòu)造一個文件流
    FileStream fs = new FileStream("../../001.jpg",FileMode.Open);
    // 使用文件流構(gòu)造一個二進制讀取器將基元數(shù)據(jù)讀作二進制值
    BinaryReader br = new BinaryReader(fs);
    XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8);
    //輸出設(shè)置 代碼縮進
    xmlTxtWt.Formatting = Formatting.Indented;
    //   xmlTxtWt.Indentation = 4;
    //書寫聲明
    xmlTxtWt.WriteStartDocument();
    xmlTxtWt.WriteStartElement("picture","ContactDetails","http://www.dbjr.com.cn");//定義命名空間
    xmlTxtWt.WriteStartElement("image");            //定義節(jié)點
    xmlTxtWt.WriteAttributeString("imageName","002.jpg");        //添加圖片屬性
    byte[] base64buffer = new byte[bytesToRead];          //開辟緩沖區(qū)
    do
    {
     readByte = br.Read(base64buffer,0,bytesToRead);      //將數(shù)據(jù)讀入字節(jié)數(shù)組
     xmlTxtWt.WriteBase64(base64buffer,0,readByte);       //將數(shù)組中二進制值編碼為Base64并寫出到XML文件
    }while(bytesToRead <= readByte);
    xmlTxtWt.WriteEndElement();
    xmlTxtWt.WriteEndElement();
    xmlTxtWt.WriteEndDocument();
//    xmlTxtWt.Flush();
    xmlTxtWt.Close();
    MessageBox.Show("讀寫結(jié)束!");
    //   this.textBox1.Text = ReadXml(fileName);
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.ToString());
   }

相關(guān)文章

最新評論