Also needed is usage and how to actually create the class
that was returned..
//
// Returns TGraphic class based on extension
//
function GetIMGType
( AFileName: String
): TGraphic;
const
IMGTypes: array[0..3] of string = ('.gif', '.jpg', '.png', '.bmp');
begin
case StringToCaseSelect(LowerCase(ExtractFileExt(AFilename)),
IMGTypes) of
0: // gif
begin
//Result := TGIFImage;
end;
1: // jpg
begin
Result := TJPEGImage;
end;
2: // png
begin
//Result := TPNG;
end;
3: // bmp
begin
Result := TBitmap;
end;
end;
end;
Apparently not correct...
Usage:
var
JPEGImage: TJPEGImage;
begin
if ClassType(GetIMGType(AFileName) = 'TJPEGImage' then
JPEGImage := TJPEGImage.Create;
.
.
end;
--
>I am trying to return the correct type of TGraphic based
>on extension of filename passed. This is not the correct
>way to do this. Any help on figuring out the correct way?
If TGIFImage, TJPEGImage etc all are TGraphic descendants (I haven't
checked), then you probably want something like this
type
TGraphicClass = class of TGraphic;
>// Returns TGraphic class based on extension
>//
>function GetIMGType
> ( AFileName: String
): TGraphicClass;
>const
> IMGTypes: array[0..3] of string = ('.gif', '.jpg', '.png', '.bmp');
>begin
> case StringToCaseSelect(LowerCase(ExtractFileExt(AFilename)),
>IMGTypes) of
> 0: // gif
> begin
> //Result := TGIFImage;
> end;
> 1: // jpg
> begin
> Result := TJPEGImage;
> end;
> 2: // png
> begin
> //Result := TPNG;
> end;
> 3: // bmp
> begin
> Result := TBitmap;
> end;
> end;
>end;
>
>
>Usage:
Probably something like
>var
Image: TGraphic;
>begin
Image:= GetIMGType(AFileName).Create;
if Image is TJPEGImage then
DoSomethingWithJPG
else ...
HTH
ain
> I am trying to return the correct type of TGraphic based
> on extension of filename passed. This is not the correct
> way to do this. Any help on figuring out the correct way?
>
> Also needed is usage and how to actually create the class
> that was returned..
>
It's a pity that the file formats registry used by the TPicture class
is internal to the Graphics unit and cannot be used in your own code.
If you cannot simply use TPicture.LoadFromFile you have to build your
own registry for graphic classes. Using an array like you are trying
now is rather unflexible, since you will have to modify the code (array
and associated function) each time you need to add a new graphics class
to support.
Anyway, the key is to have your GetImgType function return a value of
type TGraphicClass. That is the predefined class reference type for
TGraphic descendents.
//
// Returns TGraphic class based on extension
//
function GetIMGType(const AFileName: String):TGraphicClass;
type
TImgDescriptor = record ext: string; classtype: TGraphicClass; end;
const
IMGTypes: array[0..3] of TImgDescriptor =
((ext:'.gif'; classtype: TGifImage),
(ext:'.jpg'; classtype: TJpegImage),
(ext:'.png'; classtype: TPngImage),
(ext:'.bmp'; classtype: TBitmap));
var
I: Integer;
begin
for I := Low(ImgTypes) to High(ImgTypes) do
if AnsiEndsText(ImgTypes[I].ext, AFileName) then begin
Result := ImgTypes[I].classtype;
Exit;
end;
raise Exception.CreateFmt('No graphic class available for file "%s"',
[aFilename]);
end;
var
Image: TGraphic;
begin
Image := GetIMGType(AFileName).Create;
try
Image.LoadFromFile(aFilename);
.....
finally
Image.Free;
end;
--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be
> Anyway, the key is to have your GetImgType function return a value of
> type TGraphicClass. That is the predefined class reference type for
> TGraphic descendents.
Well, Peter, once again you show your genius.
Thanks a lot.
--