Imports System.Drawing
Imports System.Drawing.Imaging

Public Class TiffViewer
    Inherits System.Web.UI.Page

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

        If Not FileUpload1.HasFile Then
            Return
        End If

        Dim ext As String = IO.Path.GetExtension(FileUpload1.FileName).ToLower()
        If ext <> ".tif" AndAlso ext <> ".tiff" Then
            Return
        End If

        Dim savedFileName As String = Guid.NewGuid().ToString() & ext
        Dim savePath As String = Server.MapPath("~/TiffFiles/" & savedFileName)

        FileUpload1.SaveAs(savePath)

        Dim pageCount As Integer
        Using img As Image = Image.FromFile(savePath)
            Dim frameDim As New FrameDimension(img.FrameDimensionsList(0))
            pageCount = img.GetFrameCount(frameDim)
        End Using

        viewerPanel.Visible = True
        hdnFileName.Value = savedFileName

        Dim script As String = String.Format("initViewer('{0}', {1});", savedFileName, pageCount)
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "initViewer", script, True)

    End Sub

End Class