在Delphi中基于内容检测图片格式(非扩展名)

type
  TGraphicFormat=(gfUnknown,gfBitmap,gfJpeg,gfPng,gfGif,gfMetafile,gfTga,gfPcx,gfTiff,gfIcon,gfCursor,gfIff,gfAni);
///<summary>检测图片格式</summary>
///<params>
/// <param name="AStream">要检测的图片数据流</param>
///</params>
///<returns>返回可以识别的图片格式代码</returns>
 
function DetectImageFormat(AStream:TStream):TGraphicFormat;overload;
var
  ABuf:array[0..7] of Byte;
  ARealType:String;
  AReaded:Integer;
begin
FillChar(ABuf,8,0);
AReaded:=AStream.Read(ABuf[0],8);
AStream.Seek(-AReaded,soFromCurrent);//回到原始位置
if (ABuf[0]=$ff) and (ABuf[1]=$d8) then//JPEG文件头标识 (2 bytes): $ff, $d8 (SOI) (JPEG 文件标识)
  Result:=gfJpeg
else if (ABuf[0]=$89) and (ABuf[1]=$50) and (ABuf[2]=$4E) and (ABuf[3]=$47) and (ABuf[4]=$0D) and (ABuf[5]=$0A) and (ABuf[6]=$1A) and (ABuf[7]=$0A) then
  Result:=gfPng//3.PNG文件头标识 (8 bytes)   89 50 4E 47 0D 0A 1A 0A
else if (ABuf[0]=$42) and (ABuf[1]=$4D) then
  Result:=gfBitmap
else if (ABuf[0]=$47) and (ABuf[1]=$49) and (ABuf[2]=$46) and (ABuf[3]=$38) and (ABuf[4] in [$37,$39]) and (ABuf[5]=$61) then
  Result:=gfGif//GIF- 文件头标识 (6 bytes)   47 49 46 38 39(37) 61 G   I   F     8   9 (7)     a
else if (ABuf[0]=$01) and (ABuf[1]=$00) and (ABuf[2]=$00) and (ABuf[3] =$00) then
  Result:=gfMetafile//EMF 01 00 00 00
else if (ABuf[0]=$01) and (ABuf[1]=$00) and (ABuf[2]=$09) and (ABuf[3] =$00) and (ABuf[4]=$00) and (ABuf[5]=$03) then
  Result:=gfMetafile//WMF 01 00 09 00 00 03
else if (ABuf[0]=$00) and (ABuf[1]=$00) and ((ABuf[2]=$02) or (ABuf[2]=$10)) and (ABuf[3]=$00) and (ABuf[4]=$00) then
  Result:=gfTga//TGA- 未压缩的前5字节   00 00 02 00 00,RLE压缩的前5字节   00 00 10 00 00
else if (ABuf[0]=$0A) then
  Result:=gfPcx //PCX - 文件头标识 (1 bytes)   0A
else if ((ABuf[0]=$4D) and (ABuf[1]=$4D)) or ((ABuf[0]=$49) and (ABuf[1]=$49)) then
  Result:=gfTiff//TIFF  - 文件头标识 (2 bytes)   4D 4D 或 49 49
else if (ABuf[0]=$00) and (ABuf[1]=$00) and (ABuf[2]=$01) and (ABuf[3]=$00)
  and (ABuf[4]=$01) and (ABuf[5]=$00) and (ABuf[6]=$20) and (ABuf[7]=$20) then
  Result:=gfIcon//ICO - 文件头标识 (8 bytes)   00 00 01 00 01 00 20 20
else if (ABuf[0]=$00) and (ABuf[1]=$00) and (ABuf[2]=$02) and (ABuf[3]=$00)
  and (ABuf[4]=$01) and (ABuf[5]=$00) and (ABuf[6]=$20) and (ABuf[7]=$20) then
  Result:=gfCursor//CUR - 文件头标识 (8 bytes)   00 00 02 00 01 00 20 20
else if (ABuf[0]=$46) and (ABuf[1]=$4F) and (ABuf[2]=$52) and (ABuf[3]=$4D) then
  Result:=gfIFF//IFF - 文件头标识 (4 bytes)   46 4F 52 4D(FORM)
else if (ABuf[0]=$52) and (ABuf[1]=$49) and (ABuf[2]=$46) and (ABuf[3]=$46) then
  Result:=gfAni//11.ANI- 文件头标识 (4 bytes)   52 49 46 46(RIFF)
else
  Result:=gfUnknown;
end;
///<summary>检测图片格式</summary>
///<params>
/// <param name="AFileName">要检测的图片文件名</param>
///</params>
///<returns>返回可以识别的图片格式代码</returns>
function DetectImageFormat(AFileName:String):TGraphicFormat;overload;
var
  AStream:TStream;
begin
AStream:=TFileStream.Create(AFileName,fmOpenRead or fmShareDenyWrite);
try
  Result:=DetectImageFormat(AStream);
finally
  FreeAndNil(AStream);
end;
end;

 

这个两个函数只用于检测格式,具体加载,需要你创建支持相应类型的文件实例,然后加载。就不写示例了。

分享到: