This project has moved. For the latest updates, please go here.

Convert HTML to JPG, PNG or BMP in ASP.NET and MVC with C# using a HTML to Image Library for .NET

The HTML to Image Converter allows you quickly take screenshots of your HTML documents and web pages. It can be easily integrated into any .NET application, either ASP.NET and MVC web applications or Windows Forms and WPF desktop applications to convert HTML to JPG, PNG, BMP or to TIFF.

When you convert to PNG you also have the option to create an image wit transparent background. You can see a HTML to Image for .NET live demo where you can experiment with all the converter settings.

HTML to Image Converter for .NET Features

The main features of the HTML to Image converter are listed below:

  • Convert any HTML5 document or web pages to images
  • Convert HTML to JPEG, PNG, BMP or TIFF output formats
  • Option to create transparent PNG images
  • Easy to integrate in any .NET application
  • Fast response and very accurate output images
  • Options to select the output image width and height
  • Full support for JavaScript, CSS3, SVG and Web Fonts
  • Zero external dependencies

C# Code Sample for ASP.NET to Convert HTML to Image

The minimal C# code to convert HTML to various image formats in ASP.NET will create a HtmlToImage object and then it will call the ConvertUrlToImage method to convert a HTML page from a given URL to image or will call the ConvertHtmlToImage to convert a HTML code from a string to an image. The ConvertHtmlToImage method also requires a base URL parameter to resolve the relative links to images and other files. The result of conversion is a System.Drawing.Image object which is saved to a memory buffer in one of the image formats supported by the .NET framework and then sent to browser for download.

// create the HTML to Image converter
HtmlToImage htmlToImageConverter = new HtmlToImage();

// set browser width
htmlToImageConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text);

// set browser height if specified, otherwise use the default
if (textBoxBrowserHeight.Text.Length > 0)
htmlToImageConverter.BrowserHeight = int.Parse(textBoxBrowserHeight.Text);

// set HTML Load timeout
htmlToImageConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text);

// set whether the resulted image is transparent (has effect only when the output format is PNG)
htmlToImageConverter.TransparentImage = (dropDownListImageFormat.SelectedValue == "PNG") ?
checkBoxTransparentImage.Checked : false;

// convert to image
System.Drawing.Image imageObject = null;
string imageFormatName = dropDownListImageFormat.SelectedValue.ToLower();
string imageFileName = String.Format("HtmlToImage.{0}", imageFormatName);

if (radioButtonConvertUrl.Checked)
{
// convert URL
string url = textBoxUrl.Text;

imageObject = htmlToImageConverter.ConvertUrlToImage(url)[0];
}
else
{
// convert HTML code
string htmlCode = textBoxHtmlCode.Text;
string baseUrl = textBoxBaseUrl.Text;

imageObject = htmlToImageConverter.ConvertHtmlToImage(htmlCode, baseUrl)[0];
}

// get the image buffer in the selected image format
byte[] imageBuffer = GetImageBuffer(imageObject);

// the image object rturned by converter can be disposed
imageObject.Dispose();

// inform the browser about the binary data format
string mimeType = imageFormatName == "jpg" ? "jpeg" : imageFormatName;
HttpContext.Current.Response.AddHeader("Content-Type", "image/" + mimeType);

// let the browser know how to open the image and the image name
HttpContext.Current.Response.AddHeader("Content-Disposition",
String.Format("attachment; filename={0}; size={1}", imageFileName, imageBuffer.Length.ToString()));

// write the image buffer to HTTP response
HttpContext.Current.Response.BinaryWrite(imageBuffer);

// call End() method of HTTP response to stop ASP.NET page processing
HttpContext.Current.Response.End();

 

You can download the HTML to Image .NET library from software website. 

Last edited Oct 5, 2016 at 6:27 AM by hiqpdf, version 8