As part of ExpertPdf HtmlToPdf Converter, there is also the ImgConverter class. This class defines a set of methods to render a image from a URL or from a HTML string. The conversion result can be a stream of bytes as byte[] object or a file on the disk. The image bytes can be further saved in a disk file or can be send a HTTP response to the client browser.
ImgConverter Save/Render Methods
The method below retrieves the image bytes from a URL. There is also a similar method which produces an System.Drawing.Image object from a specified URL. The second parameter allows you to specify the format of the resulted image as a value from the System.Drawing.Imaging.ImageFormat enumeration.
The URL must be anonymously accessible from the computer running your application otherwise a ‘Get web page content cancelled or invalid URL supplied’ exception is thrown by the converter. The best way to debug this type of exception is to load the URL in the Internet Explorer browser running on the same machine with your application and see if the page is correctly loaded.
public byte[] GetImageFromUrlBytes (string url,ImageFormat format)
public Image GetImageFromUrl (string url,ImageFormat format)
To convert a HTML string to image you can use one of the following methods below. The first method simply renders the HTML string as a Image object or as a byte[]. The second one accepts an additional parameter urlBase which is the full URL of the page from where you have retrieved the HTML string. The urlBase parameter is a hint for the converter which is used to determine the full URL of the images and other external files like CSS and JavaScript referenced in the HTML string by a relative URL. If you don’t set this parameter the images referenced by relative URLS won’t appear in the document and the styles from external CSS files won’t be applied to the rendered image.
You can notice there are similar methods producing a System.Drawing.Image object instead of a byte[].
public Image GetImageFromHtmlString (string htmlString,ImageFormat format)
public Image GetImageFromHtmlString (string htmlString,ImageFormat format,string urlBase)
public byte[] GetImageBytesFromHtmlString (string htmlString,ImageFormat format)
public byte[] GetImageBytesFromHtmlString (string htmlString,ImageFormat format,string urlBase)
The correspondent methods you can use to render the image in disk file are listed below. These methods internally use the methods above to get the bytes array and then they simply save the bytes in the specified file on disk.
public void SaveImageFromUrlToFile (string url, ImageFormat format, string outFile)
public void SaveImageFromHtmlStringToFile (string htmlString, ImageFormat format, string outFile)
public void SaveImageFromHtmlStringToFile (string htmlString, ImageFormat format, string outFile, string urlBase)
In the full API reference document you’ll notice some other similar methods for converting a HTML stream to image or a HTML file to image file but they are derived from the methods described above and in the most of the cases you won’t need them.
ImgConverter Configuration Properties
The conversion process and the aspect of the generated image can be configured with the configuration properties below.
To set the license key you received after purchase and unlock the product you can use the LicenseKey property. If this property is not set with any value the converter will enter in demo mode.
public string LicenseKey { get; set; }
The PageWidth and PageHeight properties allows you to set the width and height of the virtual browser windows. The web page content is rendered based on the virtual browser width specified as a integer value in pixels. Setting these properties has the same effect as the effect produced when resizing a web page in a browser window to the specified dimensions.
The default value of the PageWidth property is 1024 pixels. The default value of the PageHeight property is 0 pixels which means the height will be automatically determined. These values are producing good results in most of the cases but there are also some situations when you’ll have to change these properties. You can also choose to let the converter auto determine both the width and height of the virtual browser by setting both PageWidth and Page Height properties to 0.
public int PageWidth { get; set; }
public int PageHeight { get; set; }
For more details about ExpertPdf visit www.html-to-pdf.net.