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

Posted on 2004-11-16
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 have a quick and easy way to determine if a file is a displayable image or not?

Thanks in advance.
Question by:bdzot
    LVL 85

    Accepted Solution

    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

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now