I am currently writing a program that creates and populates controls in a WPF application from an XML file. One of the features is that the User can select the image that he wants to display in the program. This image is displayed in the image control. After selecting the image, the program saves all the data back to the XML file.
The image is converted and saved as follows:
byte[] bytes = new byte[1];
MemoryStream ms = new MemoryStream();
System.Drawing.Image image = new Bitmap(sPathOfImage);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
bytes = ms.ToArray();
XElement Image = new XElement("Image", Convert.ToBase64String(bytes));
xDocument.Add(Image);
xDocument.Save("xDocument.xml");
And it works great, everything is fine. However, the main problem is the length of the XElement Image value . Even for 40 kilobyte images, the length is 60,000 characters. And for a 9 MB image, he needs a whopping 13,200,000 characters. And now I'm looking for a better solution for storing images in an XML file.
Through specifications, it should be in a single XML file.
So, is there a good way to make the string smaller, more suitable stream, something that I am missing? Any hints are welcome.
source
share