How to load PNG as `byte[]` to HtmlImageLoadEventArgs callback?

Feb 16, 2015 at 2:32 PM
Edited Feb 19, 2015 at 10:28 AM
I have a PNG from a database as byte[]. How can I set as a HTML image? I use this for PDF creation and the wiki/documentation is sadly lacking...
public void HandleImageLoad(object sender, HtmlImageLoadEventArgs args)
{
      var img = LoadFromSource();
        if(img == null)
        {
            args.Handled = false;
            args.Callback();
            Log.Error("Missing image: {0}", args.Src);
        }
        else
        {
            args.Handled = true;
            args.Callback(img.Data);
        }                                       
}
Feb 19, 2015 at 10:27 AM
Edited Feb 19, 2015 at 10:27 AM
For future reference:
The trick is to create a GDI image from a MemoryStream of the PNG picture. That can then be used to create a XImage as callback parameter.
using(var mem = new MemoryStream(img.Data))
{
    System.Drawing.Image sysImg = System.Drawing.Image.FromStream(mem, false, true);
    XImage ximg = XImage.FromGdiPlusImage(sysImg);
    args.Callback(ximg);
}