Convert Url to Image | |
Using SelectPdf Html to Image Converter for .NET any web page can be converted to a PNG image. 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.
This sample shows how to use SelectPdf html to image converter to convert an url to a PNG image, also setting a few properties.
// read parameters from the webpage string url = "https://www.example.com"; int webPageWidth = 1024; try { webPageWidth = Convert.ToInt32(widthValue); } catch { } int webPageHeight = 0; try { webPageHeight = Convert.ToInt32(heightValue); } catch { } // instantiate a html to image converter object HtmlToImage imgConverter = new HtmlToImage(); // set converter options imgConverter.WebPageWidth = webPageWidth; imgConverter.WebPageHeight = webPageHeight; // convert the url to a PNG image (returned as a byte array) byte[] imageBytes = imgConverter.ConvertUrl(url); // save the image to a file System.IO.File.WriteAllBytes("image.png", imageBytes);