Delphi圖片格式判斷 -开发者知识库

Delphi圖片格式判斷 -开发者知识库,第1张

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;

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复