Have you met this situation? You use an HTML converter to transform HTML to image file format. Everything works fine in your C# project, but the size of output image is not as expected. Then, you set the size in input HTML, but it still not works. Or you may get a blur image sometimes. So, you may ask how can I change the size of rendered image? How to specify image size when converting HTML to image file? Actually, it is not a hard task. Just try to use a powerful HTML converter, input an HTML URL or local file, and output an image with a specific size.

One of the most powerful and affordable C# HTML to image converter on the market is XsHtml .NET HTML to PDF SDK. Fast and precise HTML conversion technology is integrated into a single and independent library dll. Besides PDF document, it also supports common raster image file formats including JPEG/JPG, BMP, and PNG. You can choose to keep the original size of web page or specify the image size as you want. With just a few lines of code, HTML pages and raw strings will be converted to high-quality images.


C# HTML to Image: Image Size Setting

The easy way to start with image size setting is to copy the C# example below to your .NET project and test it directly. Please note, if you set the dimension (width/height) value to 0, it will keeps the original image size (width/height). If your value is larger than the original one, there is an extra blank on the rendered image accordingly.

string html = @"sample.html";
string savePath = "sample.jpg";

// Create a Html converter instance 
HtmlToImageConverter converter = new HtmlToImageConverter();

// Set output image size
// If the width and height set to 0, the output image will be rendered as original size
converter.ImageHeight = 500;
converter.ImageWidth = 800;

// Convert html to image
converter.ConvertFromFile(html, savePath);

Notice - If you use the free trial version of .NET HTML to PDF SDK, the image quality is 50% of its original.