Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1030447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:42:07+00:00 2026-05-16T12:42:07+00:00

I have an image resized program and it works. The problem is when a

  • 0

I have an image resized program and it works. The problem is when a user selects a non-image file in the file select dialog, it crashes. How can I check for image files?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:42:07+00:00Added an answer on May 16, 2026 at 12:42 pm

    UPDATE: 2022-04-05

    Since it may not be feasible to validate the binary structure of every supported image, the fastest way to check if a file contains an image is to actually load it. If it loads successfully, then it is valid. If it doesn’t then it is not.

    The code below can be used to check if a file contains a valid image or not. This code is updated to prevent locking the file while the method is called. It also handles resource disposal after the tests (thanks for pointing out this issue user1931470).

    Public Function IsValidImage(fileName As String) As Boolean
        Dim img As Drawing.Image = Nothing
        Dim isValid = False
    
        Try
            ' Image.FromFile locks the file until the image is disposed.
            ' This might not be the wanted behaviour so it is preferable to
            ' open the file stream and read the image from it.
            Using stream = New System.IO.FileStream(fileName, IO.FileMode.Open)
                img = Drawing.Image.FromStream(stream)
                isValid = True
            End Using
    
        Catch oome As OutOfMemoryException
            ' Image.FromStream throws an OutOfMemoryException
            ' if the file does not have a valid image format.
            isValid = False
    
        Finally
            ' clean up resources
            If img IsNot Nothing Then img.Dispose()
        End Try
    
        Return isValid
    End Function
    

    ORIGINAL ANSWER


    ⚠️⚠️ WARNING ⚠️⚠️

    This code has a bug that causes a high memory consumption when called several times in a program’s lifetime.

    DO NOT USE THIS CODE!!

    Here’s the VB.NET equivalent of 0xA3’s answer since the OP insisted on a VB version.

    Function IsValidImage(filename As String) As Boolean
        Try
            Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
        Catch generatedExceptionName As OutOfMemoryException
            ' Image.FromFile throws an OutOfMemoryException  
            ' if the file does not have a valid image format or 
            ' GDI+ does not support the pixel format of the file. 
            ' 
            Return False
        End Try
        Return True
    End Function
    

    You use it as follows:

    If IsValidImage("c:\path\to\your\file.ext") Then
        'do something
        '
    Else
        'do something else
        '
    End If
    

    Edit:
    I don’t recommend you check file extensions. Anyone can save a different file (text document for instance) with a .jpg extension and trick you app into beleiving it is an image.

    The best way is to load the image using the function above or to open the first few bytes and check for the JPEG signature.

    You can find more information about JPEG files and their headers here:

    • http://www.fastgraph.com/help/jpeg_header_format.html
    • http://en.wikipedia.org/wiki/JPEG
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 507k
  • Answers 507k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's certainly less well-known than some of the other projects… May 16, 2026 at 4:01 pm
  • Editorial Team
    Editorial Team added an answer The #1 problem with SQL Profiler is that doing simple… May 16, 2026 at 4:01 pm
  • Editorial Team
    Editorial Team added an answer I am guessing that starting the server which is a… May 16, 2026 at 4:01 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In a PyQT4 program, I have a QLabel displaying an image with the following
I have a website going that takes a user's uploaded image, and makes three
i have a question. There is application class in my program. It is inherited
I have this JPEG image , that opens fine in Picasa, Photoshop, web browser,
I've got a problem making alpha PNGs with PHP GD. I don't have imageMagik
I have a method to save an image, which is meant to deal gracefully
I have the following code which builds a JQueryUI Dialog popup. $j('<div></div>') .html(message) .dialog({
I am attempting to overlay images on top of a background image using the
I have about 45 decently large images (about 680x1000) that need to be loaded
I am developing a program which sends images from one computer to another (similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.