Delphi判断一个文件是不是JPG图片文件
function IsJpegFile(FileName: string): Boolean;constRightBuf : array[0..3] of Byte = ($FF,$D8,$FF,$D9);varBuf: array[0..3] of Byte;beginFillChar(Buf, 4, 0);with TFileStream.Create(FileName, 0) do begin Position := 0; ReadBuffer(Buf[0], 2); Position := Size-2; ReadBuffer(Buf[2], 2); Free;end;Result := CompareMem(@RightBuf[0], @Buf[0], 4);end;
procedure TForm1.Button1Click(Sender: TObject);//测试beginif Self.OpenDialog1.Execute then if IsJpegFile(Self.OpenDialog1.FileName) then Showmessage('Is Jpg File');end;