Imports System.Drawing.Imaging
Imports System.Web.Configuration

Public Class convert_html_code_to_image
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
        Handles Me.Load
        If Not IsPostBack Then
            TxtHtmlCode.Text = "<html>" & vbCr & vbLf & " <body>" & vbCr & vbLf & _
                "  <br/>" & vbCr & vbLf & "  Hello World from selectpdf.com." _
                & vbCr & vbLf & "  <br/>" & vbCr & vbLf & "  <br/>" & vbCr & vbLf _
                & "  <br/>" & vbCr & vbLf & "  <br/>" & vbCr & vbLf & _
                "  Hello World from selectpdf.com." & vbCr & vbLf & "  <br/>" _
                & vbCr & vbLf & "  <br/>" & vbCr & vbLf & " </body>" _
                & vbCr & vbLf & "</html>" & vbCr & vbLf
        End If
    End Sub

    Protected Sub BtnSubmit_Click(sender As Object, e As EventArgs)

            Dim renderingEngine As RenderingEngine = CType([Enum].Parse(GetType(RenderingEngine), DdlRenderingEngine.SelectedValue, True), RenderingEngine)
        ' read parameters from the webpage
        Dim htmlString As String = TxtHtmlCode.Text
        Dim baseUrl As String = TxtBaseUrl.Text

        Dim image_format As String = DdlImageFormat.SelectedValue
        Dim imgFormat As ImageFormat = ImageFormat.Png
        If image_format = "jpg" Then
            imgFormat = ImageFormat.Jpeg
        ElseIf image_format = "bmp" Then
            imgFormat = ImageFormat.Bmp
        End If

        Dim webPageWidth As Integer = 1024
        Try
            webPageWidth = Convert.ToInt32(TxtWidth.Text)
        Catch
        End Try

        Dim webPageHeight As Integer = 0
        Try
            webPageHeight = Convert.ToInt32(TxtHeight.Text)
        Catch
        End Try

        ' instantiate a html to image converter object
        Dim imgConverter As New HtmlToImage()
        imgConverter.RenderingEngine = renderingEngine

        ' set converter options
        imgConverter.WebPageWidth = webPageWidth
        imgConverter.WebPageHeight = webPageHeight

        ' create a new image converting an url
        Dim image As System.Drawing.Image = _
            imgConverter.ConvertHtmlString(htmlString, baseUrl)

        ' send image to browser
        Response.Clear()
        Response.ClearHeaders()
        Response.AddHeader("Content-Type", "image/" + imgFormat.ToString().ToLower())
        Response.AppendHeader("content-disposition", _
                              "attachment;filename=""image." + image_format + """")
        image.Save(Response.OutputStream, imgFormat)
        Response.[End]()
    End Sub
End Class