Einzelnen Beitrag anzeigen

Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#4

AW: Bildformat anhand Header erkennen

  Alt 30. Okt 2010, 01:05
Habe etwas experimentiert:

Delphi-Quellcode:
const
  GIF_HEADER = AnsiChar($47) + AnsiChar($49) + AnsiChar($46);
  JPG_HEADER = AnsiChar($FF) + AnsiChar($D8) + AnsiChar($FF) + AnsiChar($E0);
  BMP_HEADER = AnsiChar($42) + AnsiChar($4D);
  PNG_HEADER = AnsiChar($89) + AnsiChar($50) + AnsiChar($4E) + AnsiChar($47);
  PSD_HEADER = AnsiChar($38) + AnsiChar($42) + AnsiChar($50) + AnsiChar($53);
  TIF_HEADER = AnsiChar($49) + AnsiChar($49) + AnsiChar($2A);
  TIF_HEADER2 = AnsiChar(77) + AnsiChar(77) + AnsiChar(00);
  WMF_HEADER = AnsiChar($D7) + AnsiChar($CD) + AnsiChar($C6) + AnsiChar($9A);

  MAX_SIGNATURES = 7;

  Signatures: array[0..MAX_SIGNATURES] of TImageSignature = (
    (ImageType: ifGIF; Name: 'Image/GIF'; Signature: GIF_HEADER),
    (ImageType: ifJPG; Name: 'Image/JPEG'; Signature: JPG_HEADER),
    (ImageType: ifPNG; Name: 'Image/PNG'; Signature: PNG_HEADER),
    (ImageType: ifBMP; Name: 'Image/BMP'; Signature: BMP_HEADER),
    (ImageType: ifTIF; Name: 'Image/TIFF'; Signature: TIF_HEADER),
    (ImageType: ifTIF; Name: 'Image/TIFF'; Signature: TIF_HEADER2),
    (ImageType: ifWMF; Name: 'Image/WMF'; Signature: WMF_HEADER),
    (ImageType: ifPSD; Name: 'Image/PSD'; Signature: PSD_HEADER));

type
  TImageType = (ifUnknown, ifJPG, ifGIF, ifBMP, ifPNG, ifPSD, ifTIF, ifWMF);

  TImageSignature = record
    ImageType: TImageType;
    Name: string;
    Signature: AnsiString;
  end;

function CheckImageSignature(AStream: TStream; Signature: AnsiString; Offset:
  Integer): Boolean;
var
  Buffer: AnsiString;
  Count: Integer;
  BytesRead: Integer;
begin
  Result := False;
  try
    Count := Length(Signature);
    AStream.Position := Offset;
    SetLength(Buffer, Count);
    BytesRead := AStream.Read(Buffer[1], Count);
    Result := (BytesRead = Count) and (Buffer = Signature);
  except
  end;
end;

function GetImageType(FileName: string): TImageSignature;
var
  Stream: TFileStream;
  MemStr: TMemoryStream;
  i: Integer;
begin
  Result.ImageType := ifUnknown;
  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    MemStr := TMemoryStream.Create;
    try
      MemStr.CopyFrom(Stream, 5);
      if MemStr.Size > 4 then
      begin
        Result.ImageType := ifUnknown;
        for i := Low(Signatures) to High(Signatures) do
        begin
          if CheckImageSignature(MemStr, Signatures[i].Signature, 0) then
          begin
            Result.ImageType := Signatures[i].ImageType;
            Result.Name := Signatures[i].Name;
            Break;
          end;
        end;
      end;
    finally
      MemStr.Free;
    end;
  finally
    Stream.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  FileName: string;
  ImgSig: TImageSignature;
begin
  if OpenDialog1.Execute then
  begin
    FileName := OpenDialog1.FileName;
    ImgSig := GetImageType(FileName);
    Label1.Caption := ImgSig.Name;
  end;
end;
Thomas

Geändert von toms (30. Okt 2010 um 01:25 Uhr)
  Mit Zitat antworten Zitat