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

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.

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 ConvertUrlToImageModel : PageModel
    {
        public void OnGet()
        {
            DdlRenderingEngine = "Chromium";
        }

        [BindProperty]
        public string TxtUrl { 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 url = TxtUrl;
            string image_format = DdlImageFormat;

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

            int webPageHeight = 0;
            try
            {
                webPageHeight = System.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);

            // create a new image converting an url
            byte[] img = imgConverter.ConvertUrl(url);

            // 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";
            }
        }
    }
}