Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HTML string to image

Tags:

html

c#

asp.net

I have a string variable which holds HTML markup. This HTML markup basically represents the email content.

Now I want to create an image from this string content which actually holds the HTML markup. I don't want to create the HTML file by writing this content into them. I just want to create an image file using this string.

Here's what I have:

string emailBody="<html><head></head><body><p>This is my text<p>...</body</html>" 

How can I create an Image from this emailBody string content?

like image 418
Sachin Avatar asked Jul 24 '13 11:07

Sachin


People also ask

Can we convert HTML to image?

Converting HTML to JPG with the Windows screen capture tool Open your HTML file in your browser or any viewable tool. Take a snapshot of an area with your screen capture tool (Snipping tool on Windows, for example). Click File > Save as. Select the location and select the Save as type as JPG.

How do you convert a string in HTML?

The parseFromString() method of the DOMParser interface converts a string which contains HTML and returns as an HTMLDocument. OUTPUT: You can see that the HTML string is converted to an HTML element as a DOM node. Now, you can use the methods of DOM nodes like appendChild() to use the result.


2 Answers

Thanks all for your responses. I used HtmlRenderer external dll (library) to achieve the same and found below code for the same.

Here is the code for this

public void ConvertHtmlToImage() {    Bitmap m_Bitmap = new Bitmap(400, 600);    PointF point = new PointF(0, 0);    SizeF maxSize = new System.Drawing.SizeF(500, 500);    HtmlRenderer.HtmlRender.Render(Graphics.FromImage(m_Bitmap),                                            "<html><body><p>This is some html code</p>"                                            + "<p>This is another html line</p></body>",                                             point, maxSize);     m_Bitmap.Save(@"C:\Test.png", ImageFormat.Png); } 
like image 185
Sachin Avatar answered Oct 11 '22 12:10

Sachin


Try the following:

using System; using System.Drawing; using System.Threading; using System.Windows.Forms;  class Program {     static void Main(string[] args)     {         var source =  @"         <!DOCTYPE html>         <html>             <body>                 <p>An image from W3Schools:</p>                 <img                      src=""http://www.w3schools.com/images/w3schools_green.jpg""                      alt=""W3Schools.com""                      width=""104""                      height=""142"">             </body>         </html>";         StartBrowser(source);         Console.ReadLine();     }      private static void StartBrowser(string source)     {         var th = new Thread(() =>         {             var webBrowser = new WebBrowser();             webBrowser.ScrollBarsEnabled = false;             webBrowser.DocumentCompleted +=                 webBrowser_DocumentCompleted;             webBrowser.DocumentText = source;             Application.Run();         });         th.SetApartmentState(ApartmentState.STA);         th.Start();     }      static void          webBrowser_DocumentCompleted(         object sender,          WebBrowserDocumentCompletedEventArgs e)     {         var webBrowser = (WebBrowser)sender;         using (Bitmap bitmap =              new Bitmap(                 webBrowser.Width,                  webBrowser.Height))         {             webBrowser                 .DrawToBitmap(                 bitmap,                  new System.Drawing                     .Rectangle(0, 0, bitmap.Width, bitmap.Height));             bitmap.Save(@"filename.jpg",                  System.Drawing.Imaging.ImageFormat.Jpeg);         }     } } 

Note: Credits should go to Hans Passant for his excellent answer on the question WebBrowser Control in a new thread which inspired this solution.

like image 37
Alex Filipovici Avatar answered Oct 11 '22 12:10

Alex Filipovici