function ImageMIMEType(Source: string): string;
var
Mem: TMemoryStream;
Buffer: Word;
i: integer;
const
IMG_CLASS_UNKNOWN = 'unknown';
IMG_CLASS_ERROR = 'error';
IMG_CLASS_JPEG = 'image/jpeg';
IMG_CLASS_BITMAP = 'image/bmp';
IMG_CLASS_GIF = 'image/gif';
IMG_CLASS_PNG = 'image/png';
IMG_CLASS_PCX = 'image/pcx';
IMG_CLASS_PSD = 'image/psd';
IMG_CLASS_RAS = 'image/ras';
IMG_CLASS_SGI = 'image/sgi';
IMG_CLASS_TIFF = 'image/tiff';
begin
Mem := TMemoryStream.Create;
try
Mem.LoadFromFile(Source,',',');
Mem.Position := 0;
if Mem.Size = 0 then
begin
//错误
Result := IMG_CLASS_ERROR;
Exit;
end;
Mem.ReadBuffer(Buffer, 2,',',');
if Buffer = $4D42 then
Result := IMG_CLASS_BITMAP
else
if Buffer = $D8FF then
Result := IMG_CLASS_JPEG
else
if Buffer = $4947 then
Result := IMG_CLASS_GIF
else
if Buffer = $050A then
Result := IMG_CLASS_PCX
else
if Buffer = $5089 then
Result := IMG_CLASS_PNG
else
if Buffer = $4238 then
Result := IMG_CLASS_PSD
else
if Buffer = $A659 then
Result := IMG_CLASS_RAS
else
if Buffer = $DA01 then
Result := IMG_CLASS_SGI
else
if Buffer = $4949 then
Result := IMG_CLASS_TIFF
else
Result := IMG_CLASS_UNKNOWN;
finally
Mem.Free; //释放内存流对象
end;
end;
关键字词: