Converting HTML text to image using C#


This post talks about converting HTML text to image. That is, saving as image, the html text as rendered in the browser!! (Unable to put it in proper words!! :@ )

The code uses an external dll (HtmlRenderer) that needs to be added in out project. HtmlRenderer namespace provides a class “HtmlRender” that provides an overloaded method “Render“. This method saves renders the html text and saves the outcome as an image.

The following code shows the usage of the method.

namespace HtmlToBmpImageDemo
     class Program
         static void Main(string[] args)
               Bitmap m_Bitmap = new Bitmap(400, 600);
               PointF point = new PointF(0, 0);
               SizeF maxSize = new System.Drawing.SizeF(500, 500);
                                              "<html><body><p>This is a shitty html code</p>"
                                              + "<p>This is another html line</p></body>", 
                                               point, maxSize);

The problem with this method is that if we include images in the HTML mark up text, the corresponding image doesn’t show the image in the created image (hope you understand the previous line!). There is yet another method of taking a screenshot of rendered html by opening a browser process in the background and then taking a screen shot of that. But I wanted a more lighter and simpler process and found this.

Hope this helps and please do let me know if there is any method of displaying images and then taking a screen shot of that rendered HTML.

This entry was posted in .NET, C#, General and tagged , , , , . Bookmark the permalink.

3 Responses to Converting HTML text to image using C#

  1. Pingback: Generate image from HTML using HTML Renderer « The Art of Dev

Leave a Reply

Your email address will not be published. Required fields are marked *