ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Test whether a file is an image
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

tcardinal

27 Posts

Posted - Mar 15 2017 :  13:29:19  Show Profile  Reply
I need to test whether a file is an image or not. Can anyone suggest the best way to do this? I do not need to display the image.

Thanks

w2m

USA
1990 Posts

Posted - Mar 15 2017 :  13:37:49  Show Profile  Reply
Unit iexBitmaps
IsKnownFormat

Declaration
function IsKnownFormat(const FileName : WideString; bIncludeVideoFiles : Boolean = False) : boolean;


Description
Returns true if the specified filename is a supported file format.
By default, this only includes image formats. Set bIncludeVideoFiles to true to include AVI, MPEG and WMV

Note: This method only checks that file extension is recognized (e.g. .JPEG of image.jpeg). To examine the content of the image to determine if it is readable use FindFileFormat

See also: IsKnownSaveFormat

Example
If IsKnownFormat('C:\test.fax') then
  ShowMessage('ok, I can load it');


Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

tcardinal

27 Posts

Posted - Mar 15 2017 :  14:01:35  Show Profile  Reply
Yes, I should have been more specific - I want to test more than the extension. I was thinking of just trying to open it in a TImageEn, but as I don't need to view I (and so don't need a TControl descendant) wondered if there was a better way. Would using TImageEnIO.LoadFromStream work? I'll give it a go.
Go to Top of Page

w2m

USA
1990 Posts

Posted - Mar 15 2017 :  14:14:48  Show Profile  Reply
You can also just try LoadFromFile then if Aborting is true, the file is not a file format supported by ImageEn or the file is defective. You do not have to view the file.
ImageEnIO.LoadFromFile(iFilename);
if ImageEnIO.Aborting then
begin
  iNotification := NotificationCenter1.CreateNotification;
  try
    iNotification.Name := 'ErrorNotification';
    iNotification.Title := 'Error Opening Image';
    iNotification.AlertBody := 'There was an error opening the image.  The image can not be opened.';
    NotificationCenter1.PresentNotification(iNotification);
  finally
    iNotification.Free;
end;

Bill Miller
Adirondack Software & Graphics
Email: w2m@hughes.net
EBook: http://www.imageen.com/ebook/
Custom Commercial ImageEn Development
Go to Top of Page

tcardinal

27 Posts

Posted - Mar 16 2017 :  05:55:20  Show Profile  Reply
Thanks Bill. That's just what I need
Go to Top of Page

xequte

38128 Posts

Posted - Mar 18 2017 :  05:38:46  Show Profile  Reply
Actually, a quicker method is to use FindFileFormat.

if FindFileFormat( sFilename, ffContentOnly ) <> ioUnknown then
ShowMessage( 'It's a supported image file!' );

https://www.imageen.com/help/FindFileFormat.html

Nigel
Xequte Software
www.xequte.com
nigel@xequte.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: