Click or drag to resize
Pdf Library for .NET

Convert Url to Image

Using SelectPdf Html to Image Converter for .NET any web page can be converted to PNG, JPEG or BMP. The web page can be specified by an external url or can be a local html file on the disk.

To convert an url (or local file) to image, HtmlToImage class exposes the ConvertUrl(String) method.

Sample Code

This sample shows how to use SelectPdf html to image converter to convert an url to PNG, JPEG or BMP, also setting a few properties.

// read parameters from the webpage
string url = TxtUrl.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();

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

// create a new image converting an url
System.Drawing.Image image = imgConverter.ConvertUrl(url);

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

These are the properties of the HtmlToImage converter. They are set before calling one of the conversion methods:

AuthenticationPassword - Specifies the password if the web page being converted requires authentication.

AuthenticationUsername - Specifies the username if the web page being converted requires authentication.

BlinkEngineLaunchMaxTries - Blink engine process launch tries. Note: This is used only by the Blink rendering engine.

BlinkEngineLaunchTimeout - Blink engine process launch timeout. Note: This is used only by the Blink rendering engine.

BlinkEnginePath - Gets or sets the full path (excluding the chrome.exe file name) of the Blink html rendering engine binaries.

BlinkEngineTemporaryFilesPath - Gets or sets the full path of a folder where Blink html rendering engine will write temporary files. Note: Write permissions need to be enabled for this folder.

BlinkMaxThreadPoolWorkers - Upper bound for the auto-tuned Blink ThreadPool worker floor. 0 disables auto-tuning. Note: This is used only by the Blink rendering engine.

CefEnginePath - Gets or sets the full path of the directory containing the SelectPdf.Cef.exe binary.

CefEngineTemporaryFilesPath - Gets or sets the full path of a folder where the CEF rendering engine will write temporary files.

ClickElementsDelayAfter - Delay in miliseconds, after element click. Note: This is used only by the Blink or Chromium rendering engine.

ClickElementsDelayBefore - Delay in miliseconds, before element click. Note: This is used only by the Blink or Chromium rendering engine.

ClickElementsSelectors - Specifies elements from the web page that will be clicked before converting the page. Note: This is used only by the Blink or Chromium rendering engine.

CustomCSS - Use this property to specify some CSS styles that will be injected into the page that is converted. Note: This is used only by the Blink or Chromium rendering engine.

DenyLocalFileAccess - A flag indicating if local files can be loaded during the conversion. The default value is False and local files can be loaded.

DrawBackground - Gets or sets a flag indicating if the web page background is rendered into the generated image. The default value for this property is true and the page background is rendered.

EnableOfflineMode - Put the browser into offline mode before navigation, so all network requests fail. Note: Honored by the Blink and Chromium rendering engines.

ExternalBrowserEndpoint - External browser service. Note: This is used only by the Blink rendering engine.

HttpCookies - Gets the collection of custom HTTP cookies used for the conversion.

HttpHeaders - Get the collection of custom HTTP headers used for the conversion.

HttpPostParameters - Gets the collection of custom HTTP POST parameters used for the conversion.

JavaScriptEnabled - Enable scripts when rendering the url or html string. Note: If the javascript requires some time to load, MinPageLoadTime property should be set to delay the conversion with the specified number of seconds and allow the javascript to run.

LazyImagesLoadingDelay - Delay per page, in miliseconds, for lazy images loading. The default value is 50 ms. Note: Honored by the Blink and Chromium rendering engines.

LazyImagesLoadingEnabled - Enables a delay mechanism to allow images to fully load. Note: Honored by the Blink and Chromium rendering engines.

LoginOptions - Handle custom page login. Note: This is used only by the Blink or Chromium rendering engine.

MaxConversionTime - Timeout in seconds for the capture phase (producing the image after the page has loaded). Applies to the Chromium rendering engine only. Default value is 60 seconds.

MaxPageLoadTime - The web page navigation timeout in seconds. Default value is 60 seconds.

MinPageLoadTime - An additional time in seconds to wait for asynchronous items to be loaded before the web page is rendered.

PluginsEnabled - A flag indicating if plugins (like Flash players) are enabled in the converter. The default value for this property is true.

PostLoadingScript - Use this property to specify some JavaScript code that will be injected into the page that is converted and executed after the page was fully loaded. Note: This is used only by the Blink or Chromium rendering engine.

PostLoadingScriptDelayAfter - Delay in miliseconds, after post page loading javascript injection. Note: This is used only by the Blink or Chromium rendering engine.

ProxyOptions - Gets a reference to an object containing the proxy settings used to access the web page that is being converted.

RenderingEngine - Gets or sets the rendering engine used by the converter to load and render the HTML. The possible values are WebKit, WebKitRestricted and Blink. The Webkit rendering engine is internal and renders similar to Apple's Safari. For Blink, Chromium binaries must be also installed.

SecureProtocol - Protocol used for secure (HTTPS) connections.

StartupMode - Use this property to specify how the conversion starts.

StartupScript - Use this property to specify some JavaScript code that will be injected into the page that is converted.

VisibleWebElementSelector - Convert only the part of the page matched by this CSS selector, for example "#content", ".infobox" or "table.data tbody". Note: Honored by the Blink and Chromium rendering engines; the WebKit engines take an element id instead.

VisibleWebElementId - Use this property to convert only a certain section of the web page, specified by the html element ID. Honored by every rendering engine; the WebKit engines take only this form.

WebGlEnabled - Controls WebGL support in the rendering engine. Note: This is used by the Chromium and Blink rendering engines.

WebPageFixedSize - Controls whether the web page is rendered with a fixed size internal browser or the size automatically extends to make the whole content visible.

WebPageHeight - Gets or sets the web page height. If the width was not set, this property has no effect.

WebPageWidth - Gets or sets the web page width.

WindowStatus - If set, the converter waits for window.status to match this string before capturing the page. Note: Honored by the Blink and Chromium rendering engines.

See Also