將一個圖片以二進制值的形式存入Xml文件中
發(fā)表時間:2024-02-16 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]try int readByte = 0; // int bytesToRead = 100; //數(shù)據(jù)緩沖區(qū)大小 string fileName = "../../WriteXml.xml"; //要打開的文件 //...
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.deltabis.com/Contact");//定義命名空間
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());
}