Test for Graphics file?

  • Thread starter Thread starter William Barnes
  • Start date Start date
W

William Barnes

Is there a built-in method available for determining if a file represents a
graphic image? I've tried using something like this:

public bool IsGraphics(FileInfo fi){

Image tempImage;

try{
tempImage = Image.FromFile(fi.FullName);
return true;
}
catch(OutOfMemoryException){
}
return false;

}

It works but there seems to be a lot of overhead with the exception handling
and all. For maximal flexibility now and in the future, I'd rather not base
it upon known graphics file extensions (.jpg, .gif, etc.). Does anyone know
of any .NET functionality that can do it better? I didn't see any static
methods in the Image class.
Thanks
William
 
William,

Short of writing parsing code to check the file contents yourself, there
is little you can do.

You might have some luck by reading the first few bytes of the file,
since most image formats have a tag at the beginning of the file indicating
what kind of file it actually is.

Hope this helps.
 
Thanks Nicholas. Guess I'll have to live with the exception-throwing
overhead.
William

Nicholas Paldino said:
William,

Short of writing parsing code to check the file contents yourself,
there is little you can do.

You might have some luck by reading the first few bytes of the file,
since most image formats have a tag at the beginning of the file
indicating what kind of file it actually is.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

William Barnes said:
Is there a built-in method available for determining if a file represents
a graphic image? I've tried using something like this:

public bool IsGraphics(FileInfo fi){

Image tempImage;

try{
tempImage = Image.FromFile(fi.FullName);
return true;
}
catch(OutOfMemoryException){
}
return false;

}

It works but there seems to be a lot of overhead with the exception
handling and all. For maximal flexibility now and in the future, I'd
rather not base it upon known graphics file extensions (.jpg, .gif,
etc.). Does anyone know of any .NET functionality that can do it better?
I didn't see any static methods in the Image class.
Thanks
William
 
Back
Top