Create Dynamic image (.gif) through a generic handler

303 Views Asked by At

I have already the algorithm created with the library Magick.Net to show a little time counter, however when I request the gif resource through the generic handler returns a static image, but when I check the test file named: "test.gif" the dynamic image works perfect. To explain me better this is the code:

MagickImageCollection image = null;
image = ImageRenderMethods.CreateGIF((DateTime)eventDate);

var path = RequestContext.HttpContext.Server.MapPath("/Content/Images");
path += "\\test.gif";
image.Write(path);

byte[] buffer = image.ToByteArray();
context.Response.ContentType = "image/gif";
context.Response.BinaryWrite(buffer);
context.Response.Flush();

So I want to know why the response of the generic handler is not a .gif dinamic instead the static result.

1

There are 1 best solutions below

0
On BEST ANSWER

To any wonder how to expose a .gif through a generic handler with Magick.Net you need to pass the MagickImageCollection object into a MemoryStream and finally this to Byte Array I.E.:

public static MemoryStream GetMemoryStreamResult(MagickImageCollection imageGif)
{
    MemoryStream ms = new MemoryStream();
    imageGif.Write(ms, MagickFormat.Gif);
    ms.Seek(0, SeekOrigin.Begin);
    return ms;
}

and the call something like:

 var imageGif = ImageRenderMethods.GetMemoryStreamResult(image);
 byte[] buffer = imageGif.ToArray();

Hope this can be useful for somebody.