'' 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,
                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("")

        '' 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 = pixelSize})
                Case ImageEncoding.Png
                    tfile = Path.GetTempFileName()
                    re.RenderToPng(tfile, New PngSettings() With {.WindowSize = pixelSize})
                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)
                        re.RenderToGcBitmap(bmp, New HtmlToImageFormat(False) With {.MaxWindowWidth = pixelSize.Width, .MaxWindowHeight = pixelSize.Height})
                        Select Case targetEncoding
                            Case ImageEncoding.Bmp
                            Case ImageEncoding.Gif
                            Case ImageEncoding.Tiff
                            Case ImageEncoding.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)
            End Using
        End If
        '' Done.
        ms.Seek(0, SeekOrigin.Begin)
        Return ms
    End Function
End Class