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?
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.
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.
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); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With