ASP.net View an image file in the browser

Hi. In my ASP.net web app I am using the following code to view a pdf file. If I want to view an image file such as a jpg or png
file what code would I use? Thanks
    Protected Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
        Try
            Dim oSelectedFile As String
            If Me.ListBox1.SelectedItem.Text <> "" Then
                oSelectedFile = Me.ListBox1.SelectedItem.Text
            Else
                Exit Sub
            End If
            'Response.Clear()
            'Dim filePath As String = "~/Uploads/" & oSelectedFile
            'Response.ContentType = "application/pdf"
            'Response.WriteFile(filePath)
            'Response.TransmitFile(filePath)

            'Dim FilePath As String = Server.MapPath("piyushsir.pdf")
            Dim FilePath As String = Server.MapPath("~/Uploads/" & oSelectedFile)
            Dim User As New System.Net.WebClient()
            Dim FileBuffer As [Byte]() = User.DownloadData(FilePath)
            If FileBuffer IsNot Nothing Then
                Response.ContentType = "application/pdf"
                Response.AddHeader("content-length", FileBuffer.Length.ToString())
                Response.BinaryWrite(FileBuffer)
            End If

        Catch ex As Exception
            'Response.Write(ex.Message)
            Me.lblErrorDeleting.Text = ex.Message & " hr556"
        End Try
    End Sub

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technology NinjaCommented:
Hi Murray,

There are couple of ways.

1. You can check the extension of the file name i.e. the last 3 character of the file name - This is an easy way but does not make sure that the file is PDF or not
2. You use any of the PDF processing library, try to open the PDF in memory and if it gives an error - assume that it is the other type of file (Or use Image processing library to check if the file is an image or not) - This is bit tough but is a sure shot way.
3. You can check the header by reading couple of bytes and determine file type - I am not sure how accurate this method would be though.

Let me know which one would you like to go with.

Regards,
Chinmay.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks Chinmay. What would I change in my code to view an image file such as a jpg?
I suppose these three lines would have to change
 
    Response.ContentType = "application/pdf"
                Response.AddHeader("content-length", FileBuffer.Length.ToString())
                Response.BinaryWrite(FileBuffer)

Open in new window

Chinmay PatelChief Technology NinjaCommented:
You are welcome Murray.
WOW.. 4 days..  I dunno how I missed this... so sorry for the delay

 Response.ContentType = "image/jpeg"
                Response.AddHeader("content-length", FileBuffer.Length.ToString())
                Response.BinaryWrite(FileBuffer)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.