SelectPdf for .NET 10 - Convert from Html Code to Image - C# / .NET 10 Sample

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

Html Code:


Base Url:

Rendering Engine:


Image Format:


Web Page Width:
px

Web Page Height:
px
(leave empty to auto detect)

WebGL enabled
(turn off for faster conversions
of pages that do not use WebGL)

Sample code · C#
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class ConvertHtmlCodeToImageModel : PageModel
    {
        public void OnGet()
        {
            DdlRenderingEngine = "Chromium";
            TxtHtmlCode = @"<html>
    <body>
        <div style='height: 200px;'>
            <br/><br/>
            <b>Hello World from selectpdf.com.</b>
        </div>
    </body>
</html>
";
        }

        [BindProperty]
        public string TxtHtmlCode { get; set; }

        [BindProperty]
        public string TxtBaseUrl { get; set; }

        [BindProperty]
        public string DdlRenderingEngine { get; set; } = "Chromium";

        public List<SelectListItem> RenderingEngines { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Chromium", Text = "Chromium" },
        };

        [BindProperty]
        public string DdlImageFormat { get; set; }

        public List<SelectListItem> ImageFormats { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "png", Text = "PNG" },
            new SelectListItem { Value = "jpeg", Text = "JPEG" },
            new SelectListItem { Value = "bmp", Text = "BMP" },
            new SelectListItem { Value = "tiff", Text = "TIFF" },
        };

        [BindProperty]
        public string TxtWidth { get; set; }

        [BindProperty]
        public string TxtHeight { get; set; }

        [BindProperty]
        public bool ChkWebGl { get; set; } = false;   // the library default; on only for pages that draw with WebGL


        public IActionResult OnPost()
        {
            RenderingEngine renderingEngine =
                SampleForm.ParseEnum(DdlRenderingEngine, RenderingEngine.Chromium);

            // read parameters from the webpage
            string htmlString = TxtHtmlCode;
            string baseUrl = TxtBaseUrl;
            string image_format = DdlImageFormat;

            int webPageWidth = 1024;
            try
            {
                webPageWidth = Convert.ToInt32(TxtWidth);
            }
            catch { }

            int webPageHeight = 0;
            try
            {
                webPageHeight = Convert.ToInt32(TxtHeight);
            }
            catch { }

            // instantiate a html to image converter object
            HtmlToImage imgConverter = new HtmlToImage();
            imgConverter.NavigationTimeout = Startup.NavigationTimeout;

            // set converter options
            imgConverter.WebPageWidth = webPageWidth;
            imgConverter.WebPageHeight = webPageHeight;
            // WebGL off skips the GPU bring-up, which makes a conversion faster
            imgConverter.WebGlEnabled = ChkWebGl;

            // the converter returns the bytes already encoded in the requested format
            imgConverter.ImageFormat = ParseFormat(image_format);

            // convert the html string to an image
            byte[] img = imgConverter.ConvertHtmlString(htmlString, baseUrl);

            // return resulted image
            FileResult fileResult = new FileContentResult(img, MimeFor(image_format));
            fileResult.FileDownloadName = "image." + image_format;
            return fileResult;
        }

        private static PdfImageFormat ParseFormat(string fmt)
        {
            switch ((fmt ?? "png").ToLowerInvariant())
            {
                case "jpg":
                case "jpeg": return PdfImageFormat.Jpeg;
                case "bmp": return PdfImageFormat.Bmp;
                case "tiff":
                case "tif": return PdfImageFormat.Tiff;
                default: return PdfImageFormat.Png;
            }
        }

        private static string MimeFor(string fmt)
        {
            switch ((fmt ?? "png").ToLowerInvariant())
            {
                case "jpg":
                case "jpeg": return "image/jpeg";
                case "bmp": return "image/bmp";
                case "tiff":
                case "tif": return "image/tiff";
                default: return "image/png";
            }
        }
    }
}