کد:
[delphi]varForm1: TForm1;JPG_HEADER: array[0..2] of byte = ($FF, $D8, $FF);GIF_HEADER: array[0..2] of byte = ($47, $49, $46);BMP_HEADER: array[0..1] of byte = ($42, $4D);PNG_HEADER: array[0..3] of byte = ($89, $50, $4E, $47);TIF_HEADER: array[0..2] of byte = ($49, $49, $2A);TIF_HEADER2: array[0..2] of byte = (77, 77, 00);
typeTImageType = (ifUnknown, ifJPG, ifGIF, ifBMP, ifPNG, ifTIF);
implementation
{$R *.dfm}
function TypeToStr(ImageType: TImageType): String;begincase ImageType ofifJPG: Result := 'Image/JPEG';ifGIF: Result := 'Image/GIF';ifPNG: Result := 'Image/PNG';ifBMP: Result := 'Image/BMP';ifTIF: Result := 'Image/TIFF';elseResult := 'Unknown Type';end;end;
function GetImageType(FileName: String): TImageType;varStream: TFileStream;MemStr: TMemoryStream;buf: integer;tmp: string;beginResult := ifUnknown;Stream := TFileStream.Create(FileName, fmOpenRead);MemStr := TMemoryStream.Create;tryMemStr.CopyFrom(Stream, 5);if MemStr.Size > 4 thenbegin// uncomment these lines to detect "unknown types"// MemStr.Position:=0;// MemStr.Read(buf,1);// showmessage(inttostr(ord(buf)));// MemStr.Read(buf,1);// showmessage(inttostr(ord(buf)));// MemStr.Read(buf,1);// showmessage(inttostr(ord(buf)));
if CompareMem(MemStr.Memory, @JPG_HEADER, SizeOf(JPG_HEADER)) thenResult := ifJPGelse if CompareMem(MemStr.Memory, @GIF_HEADER, SizeOf(GIF_HEADER)) thenResult := ifGIFelse if CompareMem(MemStr.Memory, @PNG_HEADER, SizeOf(PNG_HEADER)) thenResult := ifPNGelse if CompareMem(MemStr.Memory, @BMP_HEADER, SizeOf(BMP_HEADER)) thenResult := ifBMPelse if CompareMem(MemStr.Memory, @TIF_HEADER, SizeOf(TIF_HEADER)) thenResult := ifTIFelse if CompareMem(MemStr.Memory, @TIF_HEADER2, SizeOf(TIF_HEADER2)) thenResult := ifTIF;end;finallyStream.Free;MemStr.Free;end;end;
procedure TForm1.Button1Click(Sender: TObject);varFileName: String;it: TImageType;beginif OpenDialog1.Execute thenbeginFileName := OpenDialog1.FileName;it := GetImageType(FileName);Label1.Caption := TypeToStr(it);end;end;
علاقه مندي ها (Bookmarks)