RenderPage0.cs
//
// This code is part of Document Solutions for Imaging demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Html;

namespace DsImagingWeb.Demos
{
    // This sample shows how to render a web page specified by a URL
    // to an image (here we render the Google home page) using DsHtml.
    //
    // DsHtml natively supports JPEG, PNG and WebP image formats.
    // For these formats, we directly use the GcHtmlRenderer class
    // to render the page to a temporary image file, which is then
    // copied to the output stream.
    //
    // Other image formats (BMP, GIF, TIFF) require rendering HTML
    // to a temporary GcBitmap, and saving that to the target format.
    //
    // Please see notes in comments at the top of HelloWorldHtml
    // sample code for details on adding DsHtml to your projects.
    public class RenderPage0
    {
        public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            // The Uri of the web page to render:
            var uri = new Uri("http://www.google.com");

            // Depending on the target format, we may need a temp file:
            string tfile = null;

            // The stream to contain the return image:
            var ms = new MemoryStream();

            // Create an instance of GcHtmlBrowser that is used to render HTML:
            using var browser = Common.Util.NewHtmlBrowser();
            // Create an HtmlPage instance rendering the source Uri:
            using var htmlPage = browser.NewPage(uri, new PageOptions() { WindowSize = pixelSize });

            // JPEG, PNG and WebP are natively supported by DsHtml. Other image formats can be
            // supported by rendering to a GcBitmap and saving it to the target format:
            switch (targetMime)
            {
                case Common.Util.MimeTypes.JPEG:
                    tfile = Path.GetTempFileName();
                    htmlPage.SaveAsJpeg(tfile, new JpegOptions());
                    break;
                case Common.Util.MimeTypes.PNG:
                    tfile = Path.GetTempFileName();
                    htmlPage.SaveAsPng(tfile, new PngOptions());
                    break;
                case Common.Util.MimeTypes.WEBP:
                    tfile = Path.GetTempFileName();
                    htmlPage.SaveAsWebp(tfile, new WebpOptions());
                    break;
                default:
                    using (var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi))
                    {
                        htmlPage.Render(bmp, new PngOptions());
                        switch (targetMime)
                        {
                            case Common.Util.MimeTypes.BMP:
                                bmp.SaveAsBmp(ms);
                                break;
                            case Common.Util.MimeTypes.GIF:
                                bmp.SaveAsGif(ms);
                                break;
                            case Common.Util.MimeTypes.TIFF:
                                bmp.SaveAsTiff(ms);
                                break;
                            case Common.Util.MimeTypes.ICO:
                                bmp.SaveAsIco(ms, null, IcoFrameEncoding.Png);
                                break;
                            default:
                                throw new Exception("Unexpected.");
                        }
                    }
                    break;
            }
            // If a temp file was used, copy the created image from the temp file and clean up:
            if (!string.IsNullOrEmpty(tfile))
            {
                using (var ts = File.OpenRead(tfile))
                    ts.CopyTo(ms);
                File.Delete(tfile);
            }
            // Done.
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }
    }
}