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();
See Also