Skip Navigation Links.
In this demo you can convert an URL or a 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.
Server Settings
IP: Port: Password:
URL
Settings
Image Format:
Browser Width: px Browser Height: px Timeout: sec
Triggering Mode:
Wait Time: sec
Skip Navigation Links
C# Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HiQPdfClient;

namespace HiQPdf_Demo
{
    public partial class ConvertHtmlToImage : System.Web.UI.Page
    {
        protected void buttonConvertToImage_Click(object sender, EventArgs e)
        {
            string serverIP = textBoxServerIP.Text;
            uint serverPort = uint.Parse(textBoxServerPort.Text);
            string serverPassword = textBoxServerPassword.Text;

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

            // use server password if necessary
            if (serverPassword.Length > 0)
                htmlToImageConverter.ServerPassword = serverPassword;

            // set a demo serial number
            htmlToImageConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // 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;

            // set triggering mode; for WaitTime mode set the wait time before convert
            switch (dropDownListTriggeringMode.SelectedValue)
            {
                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;
            }

            // convert to image
            byte[] imageBuffer = null;
            string imageFormatName = dropDownListImageFormat.SelectedValue.ToLower();
            string imageFileName = String.Format("HtmlToImage.{0}", imageFormatName);

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

                imageBuffer = htmlToImageConverter.ConvertUrlToMemory(url, GetSelectedImageFormat());
            }
            else
            {
                // convert HTML code
                string htmlCode = textBoxHtmlCode.Text;
                string baseUrl = textBoxBaseUrl.Text;

                imageBuffer = htmlToImageConverter.ConvertHtmlToMemory(htmlCode, baseUrl, GetSelectedImageFormat());
            }

            // 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();
        }

        private ImageFormats GetSelectedImageFormat()
        {
            switch (dropDownListImageFormat.SelectedValue)
            {
                case "PNG":
                    return ImageFormats.Png;
                case "JPG":
                    return ImageFormats.Jpeg;
                case "BMP":
                    return ImageFormats.Bmp;
                default:
                    return ImageFormats.Png;
            }
        }

        protected void dropDownListImageFormat_SelectedIndexChanged(object sender, EventArgs e)
        {
            checkBoxTransparentImage.Visible = dropDownListImageFormat.SelectedValue == "PNG";
        }

        protected void radioButtonConvertUrl_CheckedChanged(object sender, EventArgs e)
        {
            panelUrl.Visible = radioButtonConvertUrl.Checked;
            panelHtmlCode.Visible = !radioButtonConvertUrl.Checked;
        }

        protected void radioButtonConvertHtmlCode_CheckedChanged(object sender, EventArgs e)
        {
            panelUrl.Visible = !radioButtonConvertHtmlCode.Checked;
            panelHtmlCode.Visible = radioButtonConvertHtmlCode.Checked;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dropDownListImageFormat.SelectedValue = "PNG";
                checkBoxTransparentImage.Visible = true;

                panelUrl.Visible = radioButtonConvertUrl.Checked;
                panelHtmlCode.Visible = !radioButtonConvertUrl.Checked;

                dropDownListTriggeringMode.SelectedValue = "WaitTime";
                panelWaitTime.Visible = true;

                Master.SelectNode("htmlToImage");

                Master.LoadCodeSample("ConvertHtmlToImage");
            }
        }

        protected void dropDownListTriggeringMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelWaitTime.Visible = dropDownListTriggeringMode.SelectedValue == "WaitTime";
        }
    }
}