''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Imaging
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Html
'' 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 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 DsHtml to your projects.
Public Class RenderPage0
Function GenerateImageStream(
ByVal targetMime As String,
ByVal pixelSize As Size,
ByVal dpi As Single,
ByVal opaque As Boolean,
Optional sampleParams As String() = Nothing) As Stream
'' The Uri of the web page to render:
Dim uri = New Uri("http://www.google.com")
'' Depending on the target format, we may need a temp file:
Dim tfile As String = Nothing
'' The stream to contain the return image:
Dim ms = New MemoryStream()
'' Create an instance of GcHtmlBrowser that is used to render HTML,
'' and an HtmlPage instance rendering the source Uri:
Using browser = Util.NewHtmlBrowser(), htmlPage = browser.NewPage(uri, New PageOptions() With {.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:
Select Case targetMime
Case MimeTypes.JPEG
tfile = Path.GetTempFileName()
htmlPage.SaveAsJpeg(tfile, New JpegOptions())
Case MimeTypes.PNG
tfile = Path.GetTempFileName()
htmlPage.SaveAsPng(tfile, New PngOptions())
Case MimeTypes.WEBP
tfile = Path.GetTempFileName()
htmlPage.SaveAsWebp(tfile, New WebpOptions())
Case Else
'' For formats other than JPEG Or PNG, create a GcBitmap And render to it
Using bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
htmlPage.Render(bmp, New PngOptions())
Select Case targetMime
Case MimeTypes.BMP
bmp.SaveAsBmp(ms)
Case MimeTypes.GIF
bmp.SaveAsGif(ms)
Case MimeTypes.TIFF
bmp.SaveAsTiff(ms)
Case MimeTypes.ICO
bmp.SaveAsIco(ms, Nothing, IcoFrameEncoding.Png)
Case Else
Throw New Exception("Unexpected.")
End Select
End Using
End Select
End Using
'' If a temp file was used, copy the created image from the temp file and clean up:
If Not String.IsNullOrEmpty(tfile) Then
Using ts = File.OpenRead(tfile)
ts.CopyTo(ms)
End Using
File.Delete(tfile)
End If
'' Done.
ms.Seek(0, SeekOrigin.Begin)
Return ms
End Function
End Class