'Is it a picture file' test.

Hi Please can someone tell me how I can check whether a file I have picked from a folder of files is a picture. I have tried to check whether the file type is png for example but haven’t found the correct way to do this. I am bringing in a set of files from a folder and they all need to be of a picture type. I do some pixel manipulation and if it isn’t a picture file there is a crash with some exception. It all works well but not all the time and I need to put in a check and message to to find what is causing the occasional crash.
Many thanks for any help. Tony

I use .loadaspicture with a try…catch around it


dim p as picture = picture.Open(f)
if p <> nil then
end if

If the resulting object is nil, it wasnt a picture…

use a filter on open files

  Dim pngType As New FileType
  pngType.Name = "image/png"
  pngType.MacType = "PNG "
  pngType.MacCreator = "ogle"
  pngType.Extensions = "png"
  Dim f As FolderItem
  dim dlg as new OpenDialog
  dlg.Filter = pngType
  dlg.MultiSelect = true
  f = dlg.ShowModal
 // your code ...

A filter will not indicate the content of a file… just that the extension matches some pre-determined pattern

If you want to open the picture in Xojo (so you don’t want unsupported types),I would just open them like Jeff mentioned and catch the error.

A more general way (on OSX) is Shelling out and do:

mdls -name kMDItemKind …/path/filename.ext

That will give you the file Type/kind
(assuming the user didn’t go through the hassle to disable Spotlight)

Thank you very much for your replies. I think I will follow up on what Jeff Tullin has suggested and see how I get on. Tony

The trick with Picture.Open is that it tells you that it is a picture it can actually read.
A file extension can be wrong.

Another option would be to use the OSX ‘file’ command in a shell.
This command reads the file header and reports back with the file type.

file -b /path/to/file.jpg

Will have the result: JPEG image data, JFIF standard 1.01