SelectPdf for .NET - Convert from Html Code to Image - VB.NET / ASP.NET Sample

This sample shows how to use SelectPdf html to image converter to convert a raw html code to PNG, JPEG or BMP, also setting a few properties.

Html Code:


Base Url:

Image Format:


Web Page Width:
px

Web Page Height:
px
(leave empty to auto detect)


Sample Code Vb.Net



Imports System.Drawing.Imaging

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

        ' 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