0

I am dynamically adding a bunch of files to a bunch of Panels in my Form.

When I show the OpenFileDialog, I allow the user to select a file. When the user selects a file, I would like to:

a) If the file is an image, display a thumbnail of that image in the Panel, and
b) If the file is _not_ an image, display its File Icon in the Panel.

I am also having trouble understanding how to differentiate between an Image file and a normal file (without doing a massive IF statement for all the different image types. Is that how you would do it?)

How can we do this? I have searched but I have not found anything that helped explain how to approach this.

Edper
  • 9,144
  • 1
  • 27
  • 46
jay_t55
  • 11,362
  • 28
  • 103
  • 174

2 Answers2

1

As to b), I've come to think that you cannot really differentiate what type a file is merely by the extension. That would indeed make too long a list. With image files, I'm allowing myself the performance "luxury" of just feeding the imaging engine (FreeImage in my case) whatever comes, and then catching exceptions.

Alexander
  • 2,457
  • 1
  • 14
  • 17
1

How to get thumbnails you can read for example here

And for checking file extension yo can just create List<string> of image file extentions and write something like this:

List<string> extensions  = ....;

if(extensions.Conatins(Path.GetExtension(_yourOpenFileDialog.FileName)))

EDIT

although to check if your file is valid image you can check it for knownheader or just try something like this Image.FromFile(....).RawFormat which will throw exception if file is not image, as Alexander suggested

Community
  • 1
  • 1
Guru Stron
  • 102,774
  • 10
  • 95
  • 132