Imports SelectPdf
Imports System.IO
Imports System.Drawing.Imaging

Namespace Controllers
    Public Class PdfToImageConverterController
        Inherits Controller

        ' GET: PdfToImageConverter
        Function Index() As ActionResult
            Return View()
        End Function

        <HttpPost>
        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' the test file
            Dim filePdf As String = Server.MapPath("~/files/selectpdf.pdf")

            ' settings
            Dim imgFormat As String = collection("DdlImageFormat")

            Dim startPage As Integer = 1
            Try
                startPage = Convert.ToInt32(collection("TxtStartPage"))
            Catch
            End Try

            Dim endPage As Integer = 0
            Try
                endPage = Convert.ToInt32(collection("TxtEndPage"))
            Catch
            End Try

            ' instantiate a pdf rasterizer (pdf to image converter) object
            Dim rasterizer As New PdfRasterizer()

            ' load PDF file
            rasterizer.Load(filePdf)

            ' set the properties
            rasterizer.StartPageNumber = startPage
            rasterizer.EndPageNumber = endPage

            ' other properties that can be set
            rasterizer.Resolution = 150
            rasterizer.ColorSpace = PdfRasterizerColorSpace.RGB

            ' convert pages to images
            Dim imgBytes As Byte()
            Dim httpHeader As String
            Dim fileName As String
            If imgFormat = "TIFF" Then
                ' TIFF image format
                imgBytes = rasterizer.ConvertToTiff()
                httpHeader = "image/tiff"
                fileName = "Image.tiff"
            Else
                ' the other image formats (PNG, JPG, BMP)
                Dim images As System.Drawing.Image() = rasterizer.ConvertToImages()

                Dim format As System.Drawing.Imaging.ImageFormat
                If imgFormat = "PNG" Then
                    format = System.Drawing.Imaging.ImageFormat.Png
                    httpHeader = "image/png"
                    fileName = "Image.png"
                ElseIf imgFormat = "JPG" Then
                    format = System.Drawing.Imaging.ImageFormat.Jpeg
                    httpHeader = "image/jpeg"
                    fileName = "Image.jpg"
                Else
                    format = System.Drawing.Imaging.ImageFormat.Bmp
                    httpHeader = "image/bmp"
                    fileName = "Image.bmp"
                End If

                imgBytes = GetImageBytes(images(0), format)
            End If

            ' return resulted image
            Dim fileResult As FileResult = New FileContentResult(imgBytes, httpHeader)
            fileResult.FileDownloadName = fileName
            Return fileResult
        End Function

        Private Function GetImageBytes(image As System.Drawing.Image,
                                       format As ImageFormat) As Byte()
            Using ms = New MemoryStream()
                image.Save(ms, format)
                Return ms.ToArray()
            End Using
        End Function
    End Class
End Namespace