Render HTML as an Image

What you can do is create an aspx page that changes the response type to be in the format you want and then put the image into the stream. I created a barcode generator that does a similar thing. Excluding all the formalities of generating the image, you’ll Page_Load will look something like this:

Bitmap FinalBitmap = new Bitmap();
MemoryStream msStream = new MemoryStream();

strInputParameter == Request.Params("MagicParm").ToString()

// Magic code goes here to generate your bitmap image.
FinalBitmap.Save(msStream, ImageFormat.Png);

Response.Clear();
Response.ContentType = "image/png";

msStream.WriteTo(Response.OutputStream);

if ((FinalBitmap != null)) FinalBitmap.Dispose();

and that’s it! Then all you have to do in your image is set the URL to be something like RenderImage.aspx?MagicParm=WooHoo or whatever you need. That way you can have it render whatever you want to specify.

Leave a Comment