HiQPdf Documentation

HTML to Image Converter

Quickly Create High Quality PDFs

The HTML to Image converter allows you to quickly create screenshots of the HTML documents. The class representing the HTML to Image converter is HiQPdfHtmlToImage.

The most important features of the HTML to Image Converter are described in detail in the topics below.

Serial Number

The SerialNumber property of the of the HiQPdf.HtmlToImage class must be set with the purchased serial number as described in the License Purchasing section. In the sample code below is set with an evaluation serial number.

Create a Transparent Image

The HtmlToImageTransparentImage property controls if the resulted image has a transparent background when converting a HTML documement without background color or background image. This property is false by default.

Browser Width

The BrowserWidth property of the HiQPdf.HtmlToImage class is a property having a very high influence against the HTML content rendering. Changing this property is basically the equivalent of resizing a web browser window when viewing a web page. By default the Browser Width is 1200 pixels and this is suitable for displaying most of the web pages.

Load HTML Timeout

The HtmlLoadedTimeout property of the HiQPdf.HtmlToImage class controls the maximum time in seconds to wait for HTML document to be loaded. The default value is 120 seconds. An exception is thrown if the HTML document cannot be loaded in HtmlLoadedTimeout seconds.

Convert Methods

The Convert methods of the of the HiQPdf.HtmlToImage class are finally called to convert HTML documents to images. Function of how the HTML document to be converted is given, by URL or by content, the following Convert methods are defined in the HiQPdf.HtmlToImage class:

HTML to Image Converter Demo

In this demo you can convert an URL, a local file or a custom HTML code to an image. You can select the image format (PNG, JPG, BMP) and the browser width in pixels. When the selected image format is PNG it is also possible to choose if the image background is transparent when the HTML document does not have a background.

Demo Source Code

C#
private void buttonConvertToImage_Click(object sender, EventArgs e)
{
    // 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 = (comboBoxImageFormat.SelectedItem.ToString() == "PNG") ?
                checkBoxTransparentImage.Checked : false;

    // set triggering mode; for WaitTime mode set the wait time before convert
    switch (comboBoxTriggeringMode.SelectedItem.ToString())
    {
        case "Auto":
            htmlToImageConverter.TriggerMode = ConversionTriggerMode.Auto;
            break;
        case "WaitTime":
            htmlToImageConverter.TriggerMode = ConversionTriggerMode.WaitTime;
            htmlToImageConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text);
            break;
        case "Manual":
            htmlToImageConverter.TriggerMode = ConversionTriggerMode.Manual;
            break;
        default:
            htmlToImageConverter.TriggerMode = ConversionTriggerMode.Auto;
            break;
    }

    this.Cursor = Cursors.WaitCursor;

    // convert to image
    Image imageObject = null;
    string imageFile = null;
    try
    {
        if (radioButtonConvertUrl.Checked)
        {
            // convert URL
            string url = textBoxUrl.Text;
            imageFile = Application.StartupPath + @"\DemoOut\" + 
                String.Format("ConvertUrl.{0}", comboBoxImageFormat.SelectedItem);

            imageObject = htmlToImageConverter.ConvertUrlToImage(url)[0];
        }
        else
        {
            // convert HTML code
            string htmlCode = textBoxHtmlCode.Text;
            string baseUrl = textBoxBaseUrl.Text;
            imageFile = Application.StartupPath + @"\DemoOut\" + 
                String.Format("ConvertHtml.{0}", comboBoxImageFormat.SelectedItem);

            imageObject = htmlToImageConverter.ConvertHtmlToImage(htmlCode, baseUrl)[0];
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Conversion failed. {0}", ex.Message));
        return;
    }
    finally
    {
        this.Cursor = Cursors.Arrow;
    }

    // save the Image object to a file
    try
    {
        imageObject.Save(imageFile, GetSelectedImageFormat());
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Conversion succeeded but cannot save image to file '{0}'. {1}", imageFile, ex.Message));
    }
    finally
    {
        imageObject.Dispose();
    }

    // open the image
    try
    {
        System.Diagnostics.Process.Start(imageFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Conversion succeeded but cannot open '{0}'. {1}", imageFile, ex.Message));
    }
}
See Also

Other Resources