Problem solve Get help with specific problems with your technologies, process and projects.

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.

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).

Dig Deeper on Win Development Resources

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.