How to Convert HTML Page to Image in C# ?
This article shows you a solution for converting html source to image or html page to image.
- Step 1: Download the Websites Screenshot .Net Component.
- Step 2: Install it on your computer. You will get the WebsitesScreenshot.dll component in the folder.
- Step 3: Open your visual studio project, add this dll in your project references and try the following sample code.
The following code sample shows how to render local html page to image (JPG, GIF, PNG, BMP and TIF image formats supported). Use the method CaptureWebpage and pass the HTML page file path to the method. You can convert HTML to JPG, HTML to GIF, HTML to PNG, HTML to BMP or HTML to TIFF using this sample.
C# .Net Code.
WebsitesScreenshot.WebsitesScreenshot _Obj; _Obj=new WebsitesScreenshot.WebsitesScreenshot(); WebsitesScreenshot.WebsitesScreenshot.Result _Result; _Result = _Obj.CaptureWebpage("C:\\test.html"); if (_Result == WebsitesScreenshot.WebsitesScreenshot.Result.Captured) { _Obj.ImageFormat = WebsitesScreenshot.WebsitesScreenshot. ImageFormats.JPG; _Obj.SaveImage("c:\\test.jpg"); } _Obj.Dispose();
VB .Net code.
Dim _Obj As New WebsitesScreenshot.WebsitesScreenshot Dim _Result As WebsitesScreenshot.WebsitesScreenshot.Result With _Obj _Result = .CaptureWebpage("C:\\test.html") If _Result = WebsitesScreenshot.WebsitesScreenshot. _ Result.Captured Then .ImageFormat = WebsitesScreenshot. _ WebsitesScreenshot.ImageFormats.JPG .SaveImage("c:\test.jpg") End If End With _Obj.Dispose()
The following code sample shows how to convert html source to image. Use the method CaptureHTML and pass the html source to the method. You can save html source as JPG, GIF, PNG, BMP and TIFF format.
C# .Net Code.
WebsitesScreenshot.WebsitesScreenshot _Obj; _Obj = new WebsitesScreenshot.WebsitesScreenshot(); WebsitesScreenshot.WebsitesScreenshot.Result _Result; _Result = _Obj.CaptureHTML ("<html><body><h1> Hello world </h1></body></html>"); if (_Result == WebsitesScreenshot.WebsitesScreenshot.Result.Captured) { _Obj.ImageFormat = WebsitesScreenshot. WebsitesScreenshot.ImageFormats.GIF; _Obj.SaveImage("c:\\test.gif"); } _Obj.Dispose();
VB .Net code.
Dim _Obj As New WebsitesScreenshot.WebsitesScreenshot Dim _Result As WebsitesScreenshot.WebsitesScreenshot.Result With _Obj _Result = .CaptureHTML _ ("<html><body><h1> Hello world </h1></body></html>") If _Result = WebsitesScreenshot. _ WebsitesScreenshot.Result.Captured Then .ImageFormat = WebsitesScreenshot. _ WebsitesScreenshot.ImageFormats.GIF .SaveImage("c:\test.gif") End If End With _Obj.Dispose()