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

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

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_url_to_image
    Inherits System.Web.UI.Page

    Protected Sub BtnSubmit_Click(sender As Object, e As EventArgs)
        ' read parameters from the webpage
        Dim url As String = TxtUrl.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.ConvertUrl(url)

        ' 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