Discussion:
Determining loaded image format
(too old to reply)
JJ
2010-12-18 16:14:06 UTC
Permalink
Hi,

I've been trying to find out what image format was loaded via
TPicture.LoadFromFile method. So far I found only two ways to do it:

1. Via TPicture.Graphic.ClassName. e.g.:
pic:= TPicture.Create;
pic.LoadFromFile(Edit1.Text);
if pic.Graphic.ClassName = 'TJPEGImage' then
begin
//process JPEG image
end;

2. Via TPicture.Graphic.ClassType. e.g.:
pic:= TPicture.Create;
pic.LoadFromFile(Edit1.Text);
if pic.Graphic.ClassType = TPNGImage then
begin
//process PNG image
end;

The first method is not so efficient since it's a string comparison.
Thus, it's slow and uh... lame. -_-
The second one is the one I'm currently using. It's much more efficient
and faster.

And so I was wondering if the second method is the most efficient one.
If there are more efficient or proper ways, please let me know.

Thanks in advance.
Maarten Wiltink
2010-12-20 08:56:49 UTC
Permalink
Post by JJ
I've been trying to find out what image format was loaded via
pic:= TPicture.Create;
pic.LoadFromFile(Edit1.Text);
if pic.Graphic.ClassName = 'TJPEGImage' then
begin
//process JPEG image
end;
pic:= TPicture.Create;
pic.LoadFromFile(Edit1.Text);
if pic.Graphic.ClassType = TPNGImage then
begin
//process PNG image
end;
The first method is not so efficient since it's a string comparison.
Thus, it's slow and uh... lame. -_-
The second one is the one I'm currently using. It's much more efficient
and faster.
And so I was wondering if the second method is the most efficient one.
If there are more efficient or proper ways, please let me know.
The Graphic property is of type TGraphic, which (so says the help) is an
abstract class and any actual graphic object will be an instance of a
derived class. Any derived class. You might derive your own.

So the real determinant is, in fact, the classtype of the instance you
find there. You can't expect some predetermined enumeration; there would
always be the possibility of some unforeseen class being used.

ClassType and ClassName return information from the classtype of the
instance. ClassName has the disadvantage that types from different units
may have the same unqualified name. Using ClassType as you do has the
advantage _and_ disadvantage that it doesn't catch derived classes. You
could derive your own graphic class from TPNGImage instead of TGraphic,
and an instance would be valid when treated as a TPNGImage instance, but
it would not pass your ClassType identity test.

Checking exact values against ClassType might be optimal for you. Or you
might want to allow the extra flexibility in '(pic.Graphic is TPNGImage)'.

Groetjes,
Maarten Wiltink

Loading...