RenderPage0.cs
//
// This code is part of GrapeCity Documents for Imaging samples.
// Copyright (c) GrapeCity, 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 GcImagingWeb.Samples
{
    // This sample shows how to render a web page specified by a URL
    // to an image (here we render the Google home page) using GcHtml.
    //
    // GcHtml natively supports only two image formats - JPEG and PNG.
    // 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 GcHtml to your projects.
    public class RenderPage0
    {
        public Stream GenerateImageStream(ImageEncoding targetEncoding, int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            // 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 a GcHtmlRenderer with the source Uri
            // (note that GcHtmlRenderer ctor and other HTML rendering methods accept either a Uri
            // specifying the HTML page to render, or a string which represents the actual HTML):
            using (var re = new GcHtmlRenderer(uri))
            {
                // JPEG and PNG are natively supported by GcHtml. Other image formats are only
                // supported by rendering to a GcBitmap and saving it to the target format:
                switch (targetEncoding)
                {
                    case ImageEncoding.Jpeg:
                        tfile = Path.GetTempFileName();
                        re.RenderToJpeg(tfile, new JpegSettings() { WindowSize = new Size(pixelWidth, pixelHeight) });
                        break;
                    case ImageEncoding.Png:
                        tfile = Path.GetTempFileName();
                        re.RenderToPng(tfile, new PngSettings() { WindowSize = new Size(pixelWidth, pixelHeight) });
                        break;
                    default:
                        // For formats other than JPEG or PNG, create a GcBitmap and render to it:
                        using (var bmp = new GcBitmap(pixelWidth, pixelHeight, true, dpiX, dpiY))
                        {
                            re.RenderToGcBitmap(bmp, new HtmlToImageFormat(false) { MaxWindowWidth = pixelWidth, MaxWindowHeight = pixelHeight });
                            switch (targetEncoding)
                            {
                                case ImageEncoding.Bmp:
                                    bmp.SaveAsBmp(ms);
                                    break;
                                case ImageEncoding.Gif:
                                    bmp.SaveAsGif(ms);
                                    break;
                                case ImageEncoding.Tiff:
                                    bmp.SaveAsTiff(ms);
                                    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;
        }
    }
}