This project is read-only.

HTML to Image, JPG, PNG, SVG - C# Code Sample for ASP.NET

There are many situation when you need to display a thumbnail of a HTML page or website in your application. You can programatically generate such thumbnails in your .NET applications using EVO HTML to Image Converter for .NET library.

This sample application for ASP.NET shows how you can can generate an image from a HTML document in just a few line of C# code. You can create either raster iamges in JPG or PNG format or a vector image in SVG format.

The EVO HTML to Image Converter features illustrated this sample application are:

  • Convert HTML pages to raster images
  • Create thumbnails of the HTML pages
  • Create transparent PNG images
  • Convert HTML pages to images in memory or to image files in a folder
  • Save the HTML pages images in various image formats
  • Does not depend on any third party tools
  • Support for .NET 2.0, .NET 4.0 frameworks and later
  • Documentation and C# samples for all the features

Building the Sample Application

To build the sample for ASP.NET you have to open the Visual Studio solution and build it in this environment. All the necessary packages will be automatically downloaded from NuGet. The sample application contains C# and to convert HTML to PDF, HTML to Image and HTML to SVG. 

C# Code Sample for ASP.NET

The C# sample code below shows how you can convert HTML pages or HTML strings to PDF in just a few lines of code. An instance of the HtmlToImageConverter class is created and used to convert HTML to Image. The result of conversion is a byte[] which is sent as response to browser.

protected void convertToImageButton_Click(object sender, EventArgs e)
// Create a HTML to Image converter object with default settings
HtmlToImageConverter htmlToImageConverter = new HtmlToImageConverter();

// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToImageConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

// Set HTML Viewer width in pixels which is the equivalent in converter of the browser window width
htmlToImageConverter.HtmlViewerWidth = int.Parse(htmlViewerWidthTextBox.Text);

// Set HTML viewer height in pixels to convert the top part of a HTML page
// Leave it not set to convert the entire HTML
if (htmlViewerHeightTextBox.Text.Length > 0)
htmlToImageConverter.HtmlViewerHeight = int.Parse(htmlViewerHeightTextBox.Text);

// Set if the created image has a transparent background
htmlToImageConverter.TransparentBackground = SelectedImageFormat() == System.Drawing.Imaging.ImageFormat.Png ? transparentBackgroundCheckBox.Checked : false;

// Set the maximum time in seconds to wait for HTML page to be loaded
// Leave it not set for a default 60 seconds maximum wait time
htmlToImageConverter.NavigationTimeout = int.Parse(navigationTimeoutTextBox.Text);

// Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed
// Set this property to 0 if you don't need to wait for such asynchcronous operations to finish
if (conversionDelayTextBox.Text.Length > 0)
htmlToImageConverter.ConversionDelay = int.Parse(conversionDelayTextBox.Text);

System.Drawing.Image[] imageTiles = null;

if (convertUrlRadioButton.Checked)
string url = urlTextBox.Text;

// Convert the HTML page given by an URL to a set of Image objects
imageTiles = htmlToImageConverter.ConvertUrlToImageTiles(url);
string htmlString = htmlStringTextBox.Text;
string baseUrl = baseUrlTextBox.Text;

// Convert a HTML string with a base URL to a set of Image objects
imageTiles = htmlToImageConverter.ConvertHtmlToImageTiles(htmlString, baseUrl);

// Save the first image tile to a memory buffer

System.Drawing.Image outImage = imageTiles[0];

// Create a memory stream where to save the image
System.IO.MemoryStream imageOutputStream = new System.IO.MemoryStream();

// Save the image to memory stream
outImage.Save(imageOutputStream, SelectedImageFormat());

// Write the memory stream to a memory buffer
imageOutputStream.Position = 0;
byte[] outImageBuffer = imageOutputStream.ToArray();

// Close the output memory stream

// Send the image as response to browser

string imageFormatName = imageFormatComboBox.SelectedValue.ToLower();

// Set response content type
Response.AddHeader("Content-Type", "image/" + (imageFormatName == "jpg" ? "jpeg" : imageFormatName));

// Instruct the browser to open the image file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}; size={1}", "HTML_to_Image." + imageFormatName, outImageBuffer.Length.ToString()));

// Write the image buffer to HTTP response

// End the HTTP response and stop the current page processing

More Information

For more information please check the EVO HTML to Image Converter home page, the EVO HTML to Image Converter Online Demo , the EVO HTML to SVG Converter Online Demo, the HTML to Image for .NET blog and EVO HTML to Image for .NET blog. You can also download the EvoPdf Nuget Package directly. 


Last edited Dec 15, 2016 at 7:34 AM by evopdf, version 15