Q

Save/load to XML while preserving a an animated GIF's file properties

Relating to your topic "Can we store an image in XML file, just like we store an image file as binary in SqlDatabase?":

I noticed that if this is extended to use GIF images, reloading the image from the XML will cause an animated, transparent GIF to become static and opaque. Is there a way to save/load to XML while preserving all file properties? Thanks. The code shown in that answer used the Bitmap class. To make the code more generic and correctly handle animated gifs, the image saving code should be modified as follows:

string data = null;
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("NMatrix.gif"));
using (MemoryStream mem = new MemoryStream())
{
  // Could determine image format based on file extension, for example.
  ImageFormat fmt = ImageFormat.Gif;
  img.Save(mem, fmt);
  data = Convert.ToBase64String(mem.ToArray());
}

And reloading:

using (MemoryStream mem = new MemoryStream(Convert.FromBase64String(data)))
{
  //Load the image from the stream, if we needed the Image object
  System.Drawing.Image img = System.Drawing.Image.FromStream(mem);
  //Dump to response.
  Response.BinaryWrite(mem.ToArray());
}

Note that instead of loading a Bitmap object, we're dealing with the generic Image class (not to be confused with the System.Web.UI.WebControls.Image).

This was first published in January 2004

Dig deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close