Ca y est j'ai retrouvé ce que je cherchais.
Ce dont je te parlais en tout premier lieu, nous l'avions déjà (récupéré plus que) codé il y a bon bout de temps, mais nous ne l'utilisons plus.
Voici ce que ca donnait dans l'idée :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| function ReadGraphic(ContentStream: TStream): TGraphic;
var
Bytes: array[0..7] of Byte; // Used for reading in the image header
begin
ContentStream.Position := 0;
ContentStream.Read(Bytes[0], SizeOf(Bytes));
// 0xFFD8 is the starting marker for a JPEG
if (Bytes[0] = $FF) and (Bytes[1] = $D8) then
Result := TJPEGImage.Create
// 'GIF' (as in the string literal) is the starting marker
// for a GIF
else if (Bytes[0] = Ord('G')) and { Do not localize}
(Bytes[1] = Ord('I')) and { Do not localize}
(Bytes[2] = Ord('F')) then { Do not localize}
begin
Result := TGIFImage.Create;
// With TGIFImage(Result) do
// Transparent := true;
end
// For ping, the first 8 bytes header is:
// (HEX) 89 50 4e 47 0d 0a 1a 0a
// (ASCII C notation) \211 P N G \r \n \032 \n
else if (Bytes[0] = $89) and
(Bytes[1] = $50) and
(Bytes[2] = $4e) and
(Bytes[3] = $47) and
(Bytes[4] = $0d) and
(Bytes[5] = $0a) and
(Bytes[6] = $1a) and
(Bytes[7] = $0a) then
Result := TPNGObject.Create
else if (Bytes[0] = Ord('B')) and { Do not localize}
(Bytes[1] = Ord('M')) then // Windows bitmap { Do not localize}
Result := TBitmap.Create
else
raise Exception.Create('Format d''image incorrect');
// Go back to the start of the file.
ContentStream.Position := 0;
Result.LoadFromStream(ContentStream);
end; |
Mais heureusement, GraphicEx est là, et c'est surement mieux fait que dans le précédent code.
En faisant le uses de GraphicEx, tu auras accès à FileFormatList, dont tu pourras appeler cette méthode :
function TFileFormatList.GraphicFromContent(Stream: TStream): TGraphicExGraphicClass;
Donc tu tentes de charger directement ton TFileStream, et si ca ne marche pas, compte tenu du nombre assez impressionnant de types d'images qu'est capable de lire GraphicEx, tu peux te dire "OK, c'est un fichier texte".
Partager