Answered by:
Bitmap to XML?

Question
-
User361191494 posted
Hi there,
I am trying to save a number of thumbnail images into a single XML file rather than having an individual thumbnail file for each image. I create the XML file as follows:
XmlDocument xdoc = new XmlDocument();
XmlElement xeRoot = xdoc.CreateElement("thumbnailcatalog");
XmlAttribute xa = xdoc.CreateAttribute("folder");
xa.InnerText = diFolder.Name;
xeRoot.Attributes.Append(xa);
xdoc.AppendChild(xeRoot);
foreach (FileInfo fi in diFolder.GetFiles())
{
if (ImageFileTypes.Contains(fi.Extension.ToLower()))
{
XmlElement xe = xdoc.CreateElement("thumbnail");
Bitmap bmp = (Bitmap)Bitmap.FromFile(fi.FullName);
MemoryStream s = new MemoryStream();
bmp.Save(s, System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
bmp = null;
s.Position = 0;
StreamReader sr = new StreamReader(s, Encoding.UTF8);
xe.InnerText = sr.ReadToEnd();
s.Dispose();
xa = xdoc.CreateAttribute("filename");
xa.InnerText = fi.Name;
xe.Attributes.Append(xa);
xeRoot.AppendChild(xe);
dThumbs.Add(xe.Attributes["filename"].InnerText.ToLower(), xe);
}
}
xdoc.Save(fiThumbs.FullName);This creates an XML file with some ugly looking characters inside my <thumbnail></thumbnai> tags, fine and good. However, the problem arises when I try to create the bitmap from the data:
MemoryStream s = new MemoryStream();
On the last line where I try to create the Bitmap from the stream, I receive a "Parameter is not valid" error. What am I doing wrong?
StreamWriter sw = new StreamWriter(s, Encoding.UTF8);
sw.Write(xe.InnerText);
s.Position = 0;
Bitmap bmp = (Bitmap)Bitmap.FromStream(s);
Thursday, April 23, 2009 4:28 PM
Answers
-
User361191494 posted
Hey guys,
Thanks for the links, but unfortunately they made things even more complicated than they needed to be.
I went back to the drawing board and came up with a much simpler solution. Here's the Pseudocode:
Encoding:
1) Save the Bitmap to a MemoryStream object.
2) Use the MemoryStream.ToArray() method to get a byte[] array
3) Loop through each byte, converting it to a Hexadecimal string representation as follows: encodedstring += bytedata[i].ToString("X").PadLeft(2, '0');
4) Add the final encoded string to the XML
Decoding:
1) Get the encoded string from the XML
2) Create a byte[] array that is half the length of the encoded string
3) Iterate through each byte and convert the corresponding substring as follows: bytedata[i] = byte.Parse( encodedstring.Substring(i*2, 2), System.Globalization.NumberStyles.HexNumber );
4) Create a MemoryStream object, passing in the bytedata array to the constructor.
5) Use the Bitmap.FromStream(MemoryStream) method to get the final Bitmap.
Not terribly sophisticated but it gets the job done. The only caveat is that, because of the hex string representation taking up two characters per byte, it doubles the size of the image representation. However, if you're only storing thumbnails, this shouldn't be too severe of an issue.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 11, 2009 1:43 PM
All replies
-
User2077524089 posted
hi ,
I am not good in this forum ,but as of my known knowledge i found some links related to your problem
Have a look at these Url's
http://www.dotnetspider.com/resources/4759-XML-Serialization-C-Part-II-Images.aspx
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/50f878e0-6091-4914-b51a-b0fb32eed817
I hope these will help u
Thursday, April 30, 2009 5:10 AM -
Thursday, April 30, 2009 5:48 AM
-
User361191494 posted
Hey guys,
Thanks for the links, but unfortunately they made things even more complicated than they needed to be.
I went back to the drawing board and came up with a much simpler solution. Here's the Pseudocode:
Encoding:
1) Save the Bitmap to a MemoryStream object.
2) Use the MemoryStream.ToArray() method to get a byte[] array
3) Loop through each byte, converting it to a Hexadecimal string representation as follows: encodedstring += bytedata[i].ToString("X").PadLeft(2, '0');
4) Add the final encoded string to the XML
Decoding:
1) Get the encoded string from the XML
2) Create a byte[] array that is half the length of the encoded string
3) Iterate through each byte and convert the corresponding substring as follows: bytedata[i] = byte.Parse( encodedstring.Substring(i*2, 2), System.Globalization.NumberStyles.HexNumber );
4) Create a MemoryStream object, passing in the bytedata array to the constructor.
5) Use the Bitmap.FromStream(MemoryStream) method to get the final Bitmap.
Not terribly sophisticated but it gets the job done. The only caveat is that, because of the hex string representation taking up two characters per byte, it doubles the size of the image representation. However, if you're only storing thumbnails, this shouldn't be too severe of an issue.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 11, 2009 1:43 PM