Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Is there a quick and easy way to determine if a file is an image?

Posted on 2004-11-16
Medium Priority
Last Modified: 2010-04-24
Howdy, all,

I received images in the course of a normal day.  However, the files do not have normal extensions (".aaa", ".aab", ".aac", etc instead of ".gif", ".jpg", ".tif", etc) and are intermixed with non-image files in an archive.  

I'm writing an app that will extract and display the images, but I want to weed out the non-image files from the directory list.

Does vb.net have a quick and easy way to determine if a file is a displayable image or not?

Thanks in advance.
Question by:bdzot
LVL 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 12595394
You could do something like this:

    Private Function isValidImage(ByVal imageFileName As String) As Boolean
        If System.IO.File.Exists(imageFileName) Then
            Dim valid As Boolean
            Dim bmp As Bitmap
                bmp = Image.FromFile(imageFileName)
                valid = True
            Catch ex As Exception
                valid = False
                If Not (bmp Is Nothing) Then
                End If
            End Try
            Return valid
            Return False
        End If
    End Function

Author Comment

ID: 12595756
Thanks, Idle.

Unfortunately, .bmp is one of the formats I don't deal with.

However, this worked:

Private Function isValid(ByVal imagePath as string) as Boolean
    Dim valid as Boolean
    Dim imageFile as Image

        imageFile = Image.FromFile(imagePath)
        valid = True
    Catch ex As Exception
        valid = false
        imageFile = Nothing    
    End Try

    Return valid

End Function

Much thanks!

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question