频道分类

Delphi图片格式判断

作者:admin 来源: 日期:2019/11/2 10:36:28 人气: 标签:

 
procedure TForm1.CheckImgType(fn: string);
var
  MyImage: TMemoryStream;
  Buffer: Word;
  i: integer;
begin
  MyImage := TMemoryStream.Create;
  try
    MyImage.LoadFromFile(fn);
    MyImage.Position := 0;
    if MyImage.Size = 0 then
    begin
      ShowMessage('错误');
      Exit;
    end;
    MyImage.ReadBuffer(Buffer, 2);
    if Buffer = $4D42 then
    begin
      ShowMessage('BMP');
    end
    else if Buffer = $D8FF then
    begin
      ShowMessage('JPEG');
    end
    else if Buffer = $4947 then
    begin
      ShowMessage('GIF');
    end
    else if Buffer = $050A then
    begin
      ShowMessage('PCX');
    end
    else if Buffer = $5089 then
    begin
      ShowMessage('PNG');
    end
    else if Buffer = $4238 then
    begin
      ShowMessage('PSD');
    end
    else if Buffer = $A659 then
    begin
      ShowMessage('RAS');
    end
    else if Buffer = $DA01 then
    begin
      ShowMessage('SGI');
    end
    else if Buffer = $4949 then
    begin
      ShowMessage('TIFF');
    end
    else //如是其他类型的文件的话,直接显示错误
    begin
      ShowMessage('ERR');
    end;

  finally
    MyImage.Free;
  end;
end;