今天找了個base64 與圖片的處理函數 附加一個寫入XML文件時的函數
private static Bitmap Base64StrToBmp(string ImgBase64Str)
...{
byte[] ImgBuffer = Convert.FromBase64String(ImgBase64Str);
MemoryStream MStream = new MemoryStream(ImgBuffer);
Bitmap Bmp = new Bitmap(MStream);
return Bmp;
}
private static string ImageToBase64Str(string ImgName)
...{
Image Img = Image.FromFile(ImgName);
System.IO.MemoryStream MStream = new System.IO.MemoryStream();
Img.Save(MStream, ImageFormat.Jpeg);
byte[] ImgBuffer = MStream.GetBuffer();
string ImgBase64Str = Convert.ToBase64String(ImgBuffer);
return ImgBase64Str;
}
下面是對XML的文檔的操作:
private void ImageToXMLFile(string ImgName)
...{
string ImgBase64Str = ImageToBase64Str(ImgName);
XmlDocument XMLDoc = new XMLDocument();
XMLDoc.AppendChild(XMLDoc.CreateElement("image"));
XmlElement elmtData = XMLDoc.CreateElement("data");
elmtData.InnerText = ImgBase64Str;
XMLDoc.DocumentElement.AppendChild(elmtData);
XMLDoc.Save(XMLFileName);
}
private Bitmap ShowImgFromXML()
...{
XmlDocument XMLDoc = new XMLDocument();
XMLDoc.Load(XMLFileName);
string ImgBase64Str = XMLDoc.DocumentElement.SelectSingleNode("data").InnerText;
Bitmap Bmp = Base64StrToBmp(ImgBase64Str);
return Bmp;
//this.pictureBox1.Image = Bmp;
}