When any TGraphic descendant registering its own graphic file format with a class procedure TPicture.RegisterFileFormat(), they're all stored in Graphics.FileFormats global variable.
Too bad that FileFormats variable is not in the "interface" section of "Graphics.pas", so I can't access it. I need to read this variable to implement a special filter for my file-list control.
Can I get that list without manual fixing the Graphics.pas's source code?
Definition: Graphic images are stored digitally using a small number of standardized graphic file formats, including bit map, TIFF, JPEG, GIF, PNG; they can also be stored as raw, unprocessed data.
The 5 Types of Digital Image Files: TIFF, JPEG, GIF, PNG, and Raw Image Files, and When to Use Each One. There are 5 main formats in which to store images. Why would you choose one over another, and what are the differences?
You are working with a file-list control, and presumably thus a list of filenames. If you don't need to know the actual TGraphic
class types that are registered, only whether a given file extension is registered or not (such as to check if a later call to TPicture.LoadFromFile()
is likely to succeed), you can use the public GraphicFileMask()
function to get a list of registered file extensions and then compare your filenames to that list. For example:
uses
SysUtils, Classes, Graphics, Masks;
function IsGraphicClassRegistered(const FileName: String): Boolean;
var
Ext: String;
List: TStringList;
I: Integer;
begin
Result := False;
Ext := ExtractFileExt(FileName);
List := TStringList.Create;
try
List.Delimiter := ';';
List.StrictDelimiter := True;
List.DelimitedText := GraphicFileMask(TGraphic);
for I := 0 to List.Count-1 do
begin
if MatchesMask(FileName, List[I]) then
begin
Result := True;
Exit;
end;
end;
finally
List.Free;
end;
end;
Or, you could simply load the file and see what happens:
uses
Graphics;
function GetRegisteredGraphicClass(const FileName: String): TGraphicClass;
var
Picture: TPicture;
begin
Result := nil;
try
Picture := TPicture.Create;
try
Picture.LoadFromFile(FileName);
Result := TGraphicClass(Picture.Graphic.ClassType);
finally
Picture.Free;
end;
except
end;
end;
Update: if you want to extract the extensions and descriptions, you can use TStringList.DelimitedText
to parse the result of the GraphicFilter()
function:
uses
SysUtils, Classes, Graphics;
function RPos(const ASub, AIn: String; AStart: Integer = -1): Integer;
var
i: Integer;
LStartPos: Integer;
LTokenLen: Integer;
begin
Result := 0;
LTokenLen := Length(ASub);
// Get starting position
if AStart < 0 then begin
AStart := Length(AIn);
end;
if AStart < (Length(AIn) - LTokenLen + 1) then begin
LStartPos := AStart;
end else begin
LStartPos := (Length(AIn) - LTokenLen + 1);
end;
// Search for the string
for i := LStartPos downto 1 do begin
if Copy(AIn, i, LTokenLen) = ASub then begin
Result := i;
Break;
end;
end;
end;
procedure GetRegisteredGraphicFormats(AFormats: TStrings);
var
List: TStringList;
i, j: Integer;
desc, ext: string;
begin
List := TStringList.Create;
try
List.Delimiter := '|';
List.StrictDelimiter := True;
List.DelimitedText := GraphicFilter(TGraphic);
i := 0;
if List.Count > 2 then
Inc(i, 2); // skip the "All" filter ...
while i <= List.Count-1 do
begin
desc := List[i];
ext := List[i+1];
j := RPos('(', desc);
if j > 0 then
desc := TrimRight(Copy(desc, 1, j-1)); // remove extension mask from description
AFormats.Add(ext + '=' + desc);
Inc(i, 2);
end;
finally
List.Free;
end;
end;
Update 2: if you are just interested in a list of registered graphic file extensions, then, assuming List
is an already created TStrings
descendant, use this:
ExtractStrings([';'], ['*', '.'], PChar(GraphicFileMask(TGraphic)), List);
The GlScene project has a unit PictureRegisteredFormats.pas that implements a hack for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With