Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check an image for errors before upload 1

Status
Not open for further replies.

VBRookie

Programmer
May 29, 2001
331
0
0
US

Hello all,

I have an image library on our intranet site that any employee can upload .JPG and .GIF files to. It auto-generates thumbnails on the fly when files are uploaded.

There are a few .JPG files that won't display when they are resized. The thumbnails display ok but when the user clicks on the thumbnail to view a larger (yet still scaled) image it doesn't display.

When I try to open the image in firefox it gives an error that says the image cannot be displayed because it contains errors. In IE it just shows a broken image ...

What'd I'd like to know is if there is a way to programmatically check an image for errors so that I can prevent corrupted image files from being uploaded?

Can this be done and if so ... how?

Many Thanks,
- VB Rookie
 
Ok I have this working now ...
Its a little complicated but I'll post it here in case anyone else needs to know ...

Basically what I ended up doing was uploading the file then opening it again to read the metadata that's attached to it. This is for primarily jpg files.

Here is a snippet of my code
Code:
Try
            mybitmap = Image.FromFile("c:\\inetpub\\[URL unfurl="true"]wwwroot\\afimagebank\\images\\originals\\"[/URL] + m_origFileName)
            Dim MyPropertyIdList As Array = mybitmap.PropertyIdList

            If MyPropertyIdList.Length < 4 And fileExt.ToUpper = ".JPG" Then
               lblOutput.Text = "<b><u>Image File Contains Errors</u></b><br>This is an invalid image file which cannot be uploaded.<br>Try to display/open the image in Internet Explorer.<br>If it does not display the image file is corrupted."
               validFile = False
               File.Delete("c:\\inetpub\\[URL unfurl="true"]wwwroot\\afimagebank\\images\\originals\\"[/URL] + m_origFileName)
               Exit Sub
            Else
               lblOutput.Text = ""
            End If
         Catch exc As Exception
         End Try

I'm checking if the length of the Property List array is less than 4. I'm doing this because I noticed that valid jpeg files had a property list array count of 8 or higher. The invalid ones only had a count of 3.

This seems to be working fine for me. It stops users from adding invalid file information to our database. This doesn't apply to GIF files which only appear to have a count of 1.

Hope this helps someone else!

Regards,
- VB Rookie
 
The paths do not seem good. You had better changed them soon. E.G.

c:\\inetpub\\ ...

The webapp name is "afimagebank" in "c:\\inetpub\\ which is localhost. If the app will be deployed on the web, then the server will not be the localhost. SIMPLY:

Replace: "c:\\inetpub\\with "~"


The above is an advice only.
Greate that you solved you problem.
 
yes i'm testing locally when I deploy it all necessary paths will be changed. Much appreciated.
 
I wanted to post an update to this. After some trial and error I've come to find out that the previous code I posted (above) will sometimes flag a valid image file as being corrupted so I've revised to code as shown below:

Code:
mybitmap = Image.FromFile("c:\\inetpub\\[URL unfurl="true"]wwwroot\\afimagebank\\images\\originals\\"[/URL] + m_origFileName)
         Dim MyPropertyIdList As Array = mybitmap.PropertyIdList

         Try
            Dim ascii = New ASCIIEncoding()
            Dim propitem As PropertyItem
            Dim propDec As Integer
            Dim idx2 As Integer = 0
            Dim propHex As String

            For Each pid As Integer In MyPropertyIdList
               propitem = mybitmap.GetPropertyItem(pid)

               If idx2 = 0 And propitem.Len < 50 Then
                  lblOutput.Text = "<b><u>Image File Contains Errors</u></b><br>This is an invalid image file which cannot be uploaded.<br>Try to display/open the image in Internet Explorer.<br>If it does not display the image file is corrupted."
                  validFile = False
                                    Exit For
               End If
               idx2 = idx2 + 1
            Next
         Catch exc As Exception
            Response.Write("Reading Image Error: " & exc.Message)
         End Try

Don't ask me why this works ... but it appears to be. I ran some tests and this appears to successfully reject corrupted image files while allowing valid ones to be uploaded ...

I hope that this helps someone.

- VB Rookie
 
Thanks for posting the solution. As you say, hopefully this will help someone in the future.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top