RenderPage0.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Drawing
  8. Imports GrapeCity.Documents.Imaging
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Html
  11.  
  12. '' This sample shows how to render a web page specified by a URL
  13. '' to an image (here we render the Google home page) using DsHtml.
  14. ''
  15. '' DsHtml natively supports only two image formats - JPEG and PNG.
  16. '' For these formats, we directly use the GcHtmlRenderer class
  17. '' to render the page to a temporary image file, which is then
  18. '' copied to the output stream.
  19. ''
  20. '' Other image formats (BMP, GIF, TIFF) require rendering HTML
  21. '' to a temporary GcBitmap, and saving that to the target format.
  22. ''
  23. '' Please see notes in comments at the top of HelloWorldHtml
  24. '' sample code for details on adding DsHtml to your projects.
  25. Public Class RenderPage0
  26. Function GenerateImageStream(
  27. ByVal targetMime As String,
  28. ByVal pixelSize As Size,
  29. ByVal dpi As Single,
  30. ByVal opaque As Boolean,
  31. Optional sampleParams As String() = Nothing) As Stream
  32.  
  33. '' The Uri of the web page to render:
  34. Dim uri = New Uri("http://www.google.com")
  35.  
  36. '' Depending on the target format, we may need a temp file:
  37. Dim tfile As String = Nothing
  38.  
  39. '' The stream to contain the return image:
  40. Dim ms = New MemoryStream()
  41.  
  42. '' Create an instance of GcHtmlBrowser that is used to render HTML,
  43. '' and an HtmlPage instance rendering the source Uri:
  44. Using browser = Util.NewHtmlBrowser(), htmlPage = browser.NewPage(uri, New PageOptions() With {.WindowSize = pixelSize})
  45. '' JPEG, PNG and WebP are natively supported by DsHtml. Other image formats can be
  46. '' supported by rendering to a GcBitmap and saving it to the target format:
  47. Select Case targetMime
  48. Case MimeTypes.JPEG
  49. tfile = Path.GetTempFileName()
  50. htmlPage.SaveAsJpeg(tfile, New JpegOptions())
  51. Case MimeTypes.PNG
  52. tfile = Path.GetTempFileName()
  53. htmlPage.SaveAsPng(tfile, New PngOptions())
  54. Case MimeTypes.WEBP
  55. tfile = Path.GetTempFileName()
  56. htmlPage.SaveAsWebp(tfile, New WebpOptions())
  57. Case Else
  58. '' For formats other than JPEG Or PNG, create a GcBitmap And render to it
  59. Using bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  60. htmlPage.Render(bmp, New PngOptions())
  61. Select Case targetMime
  62. Case MimeTypes.BMP
  63. bmp.SaveAsBmp(ms)
  64. Case MimeTypes.GIF
  65. bmp.SaveAsGif(ms)
  66. Case MimeTypes.TIFF
  67. bmp.SaveAsTiff(ms)
  68. Case MimeTypes.ICO
  69. bmp.SaveAsIco(ms, Nothing, IcoFrameEncoding.Png)
  70. Case Else
  71. Throw New Exception("Unexpected.")
  72. End Select
  73. End Using
  74. End Select
  75. End Using
  76. '' If a temp file was used, copy the created image from the temp file and clean up:
  77. If Not String.IsNullOrEmpty(tfile) Then
  78. Using ts = File.OpenRead(tfile)
  79. ts.CopyTo(ms)
  80. End Using
  81. File.Delete(tfile)
  82. End If
  83. '' Done.
  84. ms.Seek(0, SeekOrigin.Begin)
  85. Return ms
  86. End Function
  87. End Class
  88.