Results 1 to 7 of 7

Thread: Fast way to check if an image file (JPG, BMP, etc) is viewable?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    2

    Question Fast way to check if an image file (JPG, BMP, etc) is viewable?

    Hi all,

    Just looking for a fast way to check a graphics file to make sure it's not corrupted. I tried using LoadPicture with an Image control (and trapping errors to detect if the file is bad) but it's slooooow.

    Any ideas would be appreciated.

    Thanks!
    Fox

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Fast way to check if an image file (JPG, BMP, etc) is viewable?

    Welcome to the forums.

    The alternative may not be faster. You would have to parse the image file manually. And that is what VB is doing before it loads the image.

    JPGs & GIFs being compressed, will be much more complicated to parse.
    Bitmaps are relatively easy to determine if they are corrupt or not.
    But if you are also talking about icons, cursors, wmfs, emfs, and GIFs then you'd need manual parsers for each of them too.

    If you are loading the image into a visible object (like a form, picbox, image control) then that will take more time because VB is displaying it after loading it.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    2

    Re: Fast way to check if an image file (JPG, BMP, etc) is viewable?

    Thanks for your reply LaVolpe.

    I've set my Image control Visible=False, does that make any difference?

    What about an external DLL/library to use for loading the images, would anything (free out there be faster than internal VB6 code?

    Thanks again,
    Fox

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Fast way to check if an image file (JPG, BMP, etc) is viewable?

    Hmmm, maybe it isn't the testing that is slow but other processes. VB can load images quite fast. And though some may not be viewable in VB it doesn't mean it is corrupted. For example, VB has real problems with modern day icons, but the icons are not corrupted. VB can't display PNG/TIFF natively but it doesn't mean the image is corrupted.

    If you give us more details of what you are trying to do and how you are doing it, we may be able to give better advice.

    Edited: Regarding DLLs, there are several; here are 2 free ones. GDI+ that comes with XP & above and free download with 98 & above from MSDN. FreeImage library is another; there's examples of its usage in the Code Bank section. Simply search there for FreeImage. But answering my question may be the best answer . Remember what I said above; even if those other DLLs say it is a valid image format, doesn't mean VB can load it.
    Last edited by LaVolpe; Feb 16th, 2010 at 09:01 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Fast way to check if an image file (JPG, BMP, etc) is viewable?

    Hmmm... this might work for you...
    Code:
    Public P As StdPicture
    
    Public Function CanLoadPicture(PathToPic) As Boolean
    
    On Error GoTo CanLoadPictureError
    
    Set P = LoadPicture(PathToPic)
    
    CanLoadPicture = True
    Exit Sub
    'if we are here then there was an error...
    End Sub
    So no delay in the rendering of the graphic and if you want to display it then...
    Code:
    Image1.Picture = P
    'Picture1.Picture = P


    Good Luck
    Option Explicit should not be an Option!

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Fast way to check if an image file (JPG, BMP, etc) is viewable?

    Quote Originally Posted by vb5prgrmr View Post
    Code:
    Public P As StdPicture
    
    Public Function CanLoadPicture(PathToPic) As Boolean
    
    On Error GoTo CanLoadPictureError
    
    Set P = LoadPicture(PathToPic)
    
    CanLoadPicture = True
    Exit Sub
    'if we are here then there was an error...
    End Sub
    On an unrelated note, this is exactly the kind of logic where On Error Resume Next is better than On Error Goto Label.
    vb Code:
    1. Public Function CanLoadPicture(PathToPic) As Boolean
    2.     On Error Resume Next
    3.     Set P = LoadPicture(PathToPic)
    4.     If Err.Number = 0
    5.         ' Worked fine
    6.     Else
    7.         ' There was an error
    8.     End If
    9. End Sub
    Better structure this way.

  7. #7
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Fast way to check if an image file (JPG, BMP, etc) is viewable?

    Opinion!... and you always return false...
    Option Explicit should not be an Option!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width