◆C# / ASP.NET SAMPLE

HTML Code to Image Converter

Paste HTML code and get a PNG, JPEG or BMP image. Free, no signup. The C# code behind this converter is below.

Html Code:


Base Url:

Image Format:


Web Page Width:
px

Web Page Height:
px
(leave empty to auto detect)

Rendering Engine:

Free to use, no signup. The images carry a “Demo Version” watermark, which a SelectPdf library license removes.

Do this in your own app

C# / VB.NET

.NET library

Install from NuGet and convert HTML to an image in a few lines.

HtmlToImage converter = new HtmlToImage();
System.Drawing.Image image =
    converter.ConvertHtmlString(htmlString, baseUrl);
image.Save("image.png", ImageFormat.Png);
See the .NET library

Free Community Edition

Free for commercial use and includes the HTML to image converter. PDFs are limited to 5 pages.

Get the Community Edition
ANY LANGUAGE

Need a PDF instead? HTML to PDF API

Convert HTML to PDF from any language. Get a free API key: 7-day trial, 200 conversions, no credit card.

curl https://selectpdf.com/api2/convert/ \
  -d "key=YOUR_KEY" \
  --data-urlencode "html=<h1>Hello World</h1>" \
  -o document.pdf
Get a free API key

About this HTML to image converter

This converter renders the HTML you paste and saves it as a PNG, JPEG or BMP image, using the same engine as the SelectPdf library for .NET. The default Chromium engine renders HTML5, CSS3 and JavaScript; the converter also offers the older WebKit engines.

The image is as wide as the browser window your HTML is laid out in, 1,024 pixels by default, and as tall as the content unless you enter a height. If the HTML loads images, style sheets or scripts by relative paths, enter the address they come from as the base URL.

Questions

Paste the HTML into the box above, choose PNG, JPEG or BMP, and click Convert To Image. The image downloads as soon as the conversion is done.
Yes. The online converter is free to use and needs no signup. The images carry a “Demo Version” watermark, which a SelectPdf library license removes.
PNG, JPEG or BMP. Choose one in the Image Format list before you convert.
The width is the width of the browser window the HTML is laid out in, and the width of the image: 1,024 pixels by default. Leave the height empty and the image is as tall as the content; enter a height to cut the image to that many pixels.
Create an HtmlToImage object, call ConvertHtmlString with your HTML and base URL, and save the image it returns. The complete C# code of this page is below.

C# / ASP.NET code for this converter

Sample code · C#
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SelectPdf.Samples
{
    public partial class convert_html_code_to_image : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TxtHtmlCode.Text = @"<html>
 <body>
  <br/>
  Hello World from selectpdf.com.
  <br/>
  <br/>
  <br/>
  <br/>
  Hello World from selectpdf.com.
  <br/>
  <br/>
 </body>
</html>
";
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            RenderingEngine renderingEngine =
                (RenderingEngine)Enum.Parse(typeof(RenderingEngine), DdlRenderingEngine.SelectedValue, true);

            // read parameters from the webpage
            string htmlString = TxtHtmlCode.Text;
            string baseUrl = TxtBaseUrl.Text;

            string image_format = DdlImageFormat.SelectedValue;
            ImageFormat imageFormat = ImageFormat.Png;
            if (image_format == "jpg")
            {
                imageFormat = ImageFormat.Jpeg;
            }
            else if (image_format == "bmp")
            {
                imageFormat = ImageFormat.Bmp;
            }

            int webPageWidth = 1024;
            try
            {
                webPageWidth = Convert.ToInt32(TxtWidth.Text);
            }
            catch { }

            int webPageHeight = 0;
            try
            {
                webPageHeight = Convert.ToInt32(TxtHeight.Text);
            }
            catch { }

            // instantiate a html to image converter object
            HtmlToImage imgConverter = new HtmlToImage();
            imgConverter.MaxPageLoadTime = Global.MaxPageLoadTime;

            imgConverter.RenderingEngine = renderingEngine;

            // set converter options
            imgConverter.WebPageWidth = webPageWidth;
            imgConverter.WebPageHeight = webPageHeight;

            // create a new image converting an url
            System.Drawing.Image image = 
                imgConverter.ConvertHtmlString(htmlString, baseUrl);

            // send image to browser
            Response.Clear();
            Response.ClearHeaders();
            Response.AddHeader("Content-Type", "image/" +
                imageFormat.ToString().ToLower());
            Response.AppendHeader("content-disposition",
                "attachment;filename=\"image." + image_format + "\"");
            image.Save(Response.OutputStream, imageFormat);
            Response.End();
        }
    }
}