delphi两张图片的对比代码
procedure TForm1.Button4Click(Sender: TObject);
var
BMP,BMP1:TBitMap;
begin
BMP:=TBitMap.Create;
BMP1:=TBitMap.Create;
BMP1.LoadFromFile('c:\\test.bmp');
BMP.LoadFromFile('c:\\test1.bmp');
//Form1.Canvas.Draw(50,50,BMP);
//BMP.Free;
IsBmpSame(BMP,BMP1);
BMP.Free;
BMP1.Free;
end;
function IsBmpSame(bmp1,bmp2: TBitmap): Boolean;
var
a,i,j: Integer;
ScanLine1,ScanLine2: PByteArray;
Count: Integer;
begin
Result := (bmp1.Height = bmp2.Height) and
(bmp1.Width = bmp2.Width) and
(bmp1.PixelFormat = bmp2.PixelFormat);
if Result then
begin
i := Integer(bmp1.PixelFormat);
if i < 4 then i := 4 else if i = 4 then inc(i);
Count := (i - 3) * bmp1.Width - 1;
for i:=0 to bmp1.Height-1 do
begin
ScanLine1 := bmp1.ScanLine[i];
ScanLine2 := bmp2.ScanLine[i];
for j := 0 to Count do
if ScanLine1[j] <> ScanLine2[j] then
begin
showmessage('图片尺寸或像素格式不一致') ;
a:= j ;
Result := False;
Exit;
end;
end;
end;
end;
上面是一段代码,都是网上找的,
procedure TForm1.Button3Click(Sender: TObject);
var
BMP,BMP1:TBitMap;
begin
BMP:=TBitMap.Create;
BMP1:=TBitMap.Create;
BMP1.LoadFromFile('c:\\test.bmp');
BMP.LoadFromFile('c:\\test1.bmp');
//Form1.Canvas.Draw(50,50,BMP);
//BMP.Free;
Compare2Line(BMP,BMP1);
BMP.Free;
BMP1.Free;
end;
function Compare2Line(pBmpMain,pBmpSub: TBitmap): Boolean;
var
b: boolean; i,j: integer;
ptr1,ptr2: Pointer;
begin
for i := 0 to pBmpMain.Height - 1 do
begin
ptr1 := pBmpMain.ScanLine[i];
ptr2 := pBmpSub.ScanLine[i];
b := CompareMem(ptr1,ptr2,pBmpMain.Width * 3);
if not b then break;
end;
Result := b;
showmessage('图片尺寸或像素格式不一致') ;
end;
这是有一段代码,