RenderPage0.vb
''
'' This code is part of GrapeCity Documents for Imaging samples.
'' Copyright (c) GrapeCity, 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 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
    Function GenerateImageStream(
                ByVal targetEncoding As ImageEncoding,
                Optional pixelWidth As Integer = 1024,
                Optional pixelHeight As Integer = 1024,
                Optional opaque As Boolean = True,
                Optional dpiX As Single = 96,
                Optional dpiY As Single = 96) 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 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 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:
            Select Case targetEncoding
                Case ImageEncoding.Jpeg
                    tfile = Path.GetTempFileName()
                    re.RenderToJpeg(tfile, New JpegSettings() With {.WindowSize = New Size(pixelWidth, pixelHeight)})
                Case ImageEncoding.Png
                    tfile = Path.GetTempFileName()
                    re.RenderToPng(tfile, New PngSettings() With {.WindowSize = New Size(pixelWidth, pixelHeight)})
                Case Else
                    '' For formats other than JPEG Or PNG, create a GcBitmap And render to it
                    Using bmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
                        re.RenderToGcBitmap(bmp, New HtmlToImageFormat(False) With {.MaxWindowWidth = pixelWidth, .MaxWindowHeight = pixelHeight})
                        Select Case targetEncoding
                            Case ImageEncoding.Bmp
                                bmp.SaveAsBmp(ms)
                            Case ImageEncoding.Gif
                                bmp.SaveAsGif(ms)
                            Case ImageEncoding.Tiff
                                bmp.SaveAsTiff(ms)
                            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