//// This code is part of Document Solutions for Imaging demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Imaging;usingGrapeCity.Documents.Html; namespaceDsImagingWeb.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.publicclassRenderPage0 {publicStreamGenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null) {// The Uri of the web page to render:var uri = newUri("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 = newMemoryStream(); // Create an instance of GcHtmlBrowser that is used to render HTML:usingvar browser = Common.Util.NewHtmlBrowser();// Create an HtmlPage instance rendering the source Uri:usingvar htmlPage = browser.NewPage(uri, newPageOptions() { 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) {caseCommon.Util.MimeTypes.JPEG: tfile = Path.GetTempFileName(); htmlPage.SaveAsJpeg(tfile, newJpegOptions());break;caseCommon.Util.MimeTypes.PNG: tfile = Path.GetTempFileName(); htmlPage.SaveAsPng(tfile, newPngOptions());break;caseCommon.Util.MimeTypes.WEBP: tfile = Path.GetTempFileName(); htmlPage.SaveAsWebp(tfile, newWebpOptions());break;default:using (var bmp = newGcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi)) { htmlPage.Render(bmp, newPngOptions());switch (targetMime) {caseCommon.Util.MimeTypes.BMP: bmp.SaveAsBmp(ms);break;caseCommon.Util.MimeTypes.GIF: bmp.SaveAsGif(ms);break;caseCommon.Util.MimeTypes.TIFF: bmp.SaveAsTiff(ms);break;caseCommon.Util.MimeTypes.ICO: bmp.SaveAsIco(ms, null, IcoFrameEncoding.Png);break;default:thrownewException("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; } }}