RenderPage0.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Drawing;
  9. using GrapeCity.Documents.Imaging;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Html;
  12.  
  13. namespace DsImagingWeb.Demos
  14. {
  15. // This sample shows how to render a web page specified by a URL
  16. // to an image (here we render the Google home page) using DsHtml.
  17. //
  18. // DsHtml natively supports JPEG, PNG and WebP image formats.
  19. // For these formats, we directly use the GcHtmlRenderer class
  20. // to render the page to a temporary image file, which is then
  21. // copied to the output stream.
  22. //
  23. // Other image formats (BMP, GIF, TIFF) require rendering HTML
  24. // to a temporary GcBitmap, and saving that to the target format.
  25. //
  26. // Please see notes in comments at the top of HelloWorldHtml
  27. // sample code for details on adding DsHtml to your projects.
  28. public class RenderPage0
  29. {
  30. public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  31. {
  32. // The Uri of the web page to render:
  33. var uri = new Uri("http://www.google.com");
  34.  
  35. // Depending on the target format, we may need a temp file:
  36. string tfile = null;
  37.  
  38. // The stream to contain the return image:
  39. var ms = new MemoryStream();
  40.  
  41. // Create an instance of GcHtmlBrowser that is used to render HTML:
  42. using var browser = Common.Util.NewHtmlBrowser();
  43. // Create an HtmlPage instance rendering the source Uri:
  44. using var htmlPage = browser.NewPage(uri, new PageOptions() { WindowSize = pixelSize });
  45.  
  46. // JPEG, PNG and WebP are natively supported by DsHtml. Other image formats can be
  47. // supported by rendering to a GcBitmap and saving it to the target format:
  48. switch (targetMime)
  49. {
  50. case Common.Util.MimeTypes.JPEG:
  51. tfile = Path.GetTempFileName();
  52. htmlPage.SaveAsJpeg(tfile, new JpegOptions());
  53. break;
  54. case Common.Util.MimeTypes.PNG:
  55. tfile = Path.GetTempFileName();
  56. htmlPage.SaveAsPng(tfile, new PngOptions());
  57. break;
  58. case Common.Util.MimeTypes.WEBP:
  59. tfile = Path.GetTempFileName();
  60. htmlPage.SaveAsWebp(tfile, new WebpOptions());
  61. break;
  62. default:
  63. using (var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi))
  64. {
  65. htmlPage.Render(bmp, new PngOptions());
  66. switch (targetMime)
  67. {
  68. case Common.Util.MimeTypes.BMP:
  69. bmp.SaveAsBmp(ms);
  70. break;
  71. case Common.Util.MimeTypes.GIF:
  72. bmp.SaveAsGif(ms);
  73. break;
  74. case Common.Util.MimeTypes.TIFF:
  75. bmp.SaveAsTiff(ms);
  76. break;
  77. case Common.Util.MimeTypes.ICO:
  78. bmp.SaveAsIco(ms, null, IcoFrameEncoding.Png);
  79. break;
  80. default:
  81. throw new Exception("Unexpected.");
  82. }
  83. }
  84. break;
  85. }
  86. // If a temp file was used, copy the created image from the temp file and clean up:
  87. if (!string.IsNullOrEmpty(tfile))
  88. {
  89. using (var ts = File.OpenRead(tfile))
  90. ts.CopyTo(ms);
  91. File.Delete(tfile);
  92. }
  93. // Done.
  94. ms.Seek(0, SeekOrigin.Begin);
  95. return ms;
  96. }
  97. }
  98. }
  99.