Mega Code Archive

 
Categories / Delphi / Graphic
 

Determine image type

Title: Determine image type Question: How to determine image type (data level) Answer: Function GetGraphicClass(Source:TStream):TGraphicClass; var FStreamHeader : TStringStream; StartPosition : Int64; CONST PNGFILE_HEADER = 'PNG'; BMPFILE_HEADER = 'BM'; JPGFILE_HEADER = ''; Begin try StartPosition := Source.Position; IF Source.Size 0 Then Source.Position := 0 else // Stream vaco Raise Exception.Create('stream is empty'); FStreamHeader := TStringStream.Create(''); with FStreamHeader do begin CopyFrom(Source,512); if pos(PNGFILE_HEADER,DataString) 0 Then Result := TPNGObject else if pos(BMPFILE_HEADER,DataString) 0 Then Result := TBitmap else if pos(JPGFILE_HEADER,DataString) 0 Then Result := TJPEGImage else Result := TGraphic; end; finally Source.Position := StartPosition; end; end;