picture file in XML

  • Thread starter Thread starter VJ
  • Start date Start date
V

VJ

I am looking for ways to store a picture file as a data element in a XML
file. Is the below the best option available, to convert a picture file to
string format..

string data = null;
Bitmap bmp = new Bitmap(@"e:Image.bmp");
using (MemoryStream mem = new MemoryStream())
{
bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Bmp);
//Convert the bytes to a string.
data = Convert.ToBase64String(mem.ToArray());
}



VJ
 
VJ,

Pretty much. The only option you have now is to convert the bytes into
a text format, and then embed that text in your XML.

Hope this helps.
 
One problem with this is that the binary data might convert to characters
outside of the standard ASCII range (i.e. control characters). Some control
characters are not valid within an XML document.

But normally when you want to store binary data in a text format, Base64
encoding is usually the way to go.

Nicholas Paldino said:
VJ,

Pretty much. The only option you have now is to convert the bytes into
a text format, and then embed that text in your XML.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

VJ said:
I am looking for ways to store a picture file as a data element in a XML
file. Is the below the best option available, to convert a picture file to
string format..

string data = null;
Bitmap bmp = new Bitmap(@"e:Image.bmp");
using (MemoryStream mem = new MemoryStream())
{
bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Bmp);
//Convert the bytes to a string.
data = Convert.ToBase64String(mem.ToArray());
}



VJ
 
Sorry, I should have clarified, I didn't mean directly into text, but
rather, a text format which accounts for something like this (like base 64).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Rilling said:
One problem with this is that the binary data might convert to characters
outside of the standard ASCII range (i.e. control characters). Some control
characters are not valid within an XML document.

But normally when you want to store binary data in a text format, Base64
encoding is usually the way to go.

message news:[email protected]...
VJ,

Pretty much. The only option you have now is to convert the bytes into
a text format, and then embed that text in your XML.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

VJ said:
I am looking for ways to store a picture file as a data element in a XML
file. Is the below the best option available, to convert a picture
file
 
I am looking for ways to store a picture file as a data element in a XML
file. Is the below the best option available, to convert a picture file to
string format..

string data = null;
Bitmap bmp = new Bitmap(@"e:Image.bmp");
using (MemoryStream mem = new MemoryStream())
{
bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Bmp);
//Convert the bytes to a string.
data = Convert.ToBase64String(mem.ToArray());
}

You could convert the file to SVG, it is an Xml image format
specifically designed for embedding.

There are a number of converts out there, don't know if there are .Net
ones yet.

Vin
 
Back
Top