Can encode/decode a Image Object to keep it in a XML file using Compact Framework?
As the title mentioned, I want to encode a Image Obj into some kind of text data (compact framework not support binaryformatter, correct me if I'm wrong). So is there any way to encode a Image Obj into text data and keep it in a XML file for being able to decode from XML file to Image obj later?
UPDATE: Here is what I did following Sam's respose. Thanks Sam!
//Write to XML
byte[] Ret;
using (MemoryStream ms = new MemoryStream())
{
myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Ret = ms.ToArray();
}
StreamWriter myWrite = new StreamWriter(myPathFile);
myWrite.Write(Convert.ToBase64String(Ret));
myWrite.Flush();
myWrite.Close();
Then when I want to decode Image from Base64String to Image:
StreamReader StrR = new StreamReader(myPathFile);
BArr = Convert.FromBase64String(StrR.ReadToEnd());
using (MemoryStream ms = new MemoryStream(BArr,0,BArr.Length))
{开发者_运维百科
ms.Write(BArr, 0, BArr.Length);
listControl1.BGImage = new Bitmap(ms);
}
Typically binary data is converted to Base64 when included in XML. Look at Convert.ToBase64String
.
精彩评论