检查两个Tbitmap是否相同的最快方法是什么?

时间:2009-08-29 20:32:53

标签: delphi delphi-2009

有没有比逐像素检查更好的方法?

3 个答案:

答案 0 :(得分:16)

您可以将两个位图保存到TMemoryStream并使用CompareMem进行比较:

function IsSameBitmap(Bitmap1, Bitmap2: TBitmap): Boolean;
var
 Stream1, Stream2: TMemoryStream;
begin
  Assert((Bitmap1 <> nil) and (Bitmap2 <> nil), 'Params can''t be nil');
  Result:= False;
  if (Bitmap1.Height <> Bitmap2.Height) or (Bitmap1.Width <> Bitmap2.Width) then
     Exit;
  Stream1:= TMemoryStream.Create;
  try
    Bitmap1.SaveToStream(Stream1);
    Stream2:= TMemoryStream.Create;
    try
      Bitmap2.SaveToStream(Stream2);
      if Stream1.Size = Stream2.Size Then
        Result:= CompareMem(Stream1.Memory, Stream2.Memory, Stream1.Size);
    finally
      Stream2.Free;
    end;
  finally
    Stream1.Free;
  end;
end;

begin
  if IsSameBitmap(MyImage1.Picture.Bitmap, MyImage2.Picture.Bitmap) then
  begin
    // your code for same bitmap
  end;
end;

我没有对此代码X扫描线进行基准测试,如果您这样做,请告诉我们哪一个是最快的。

答案 1 :(得分:12)

使用ScanLine,不使用TMemoryStream。

function IsSameBitmapUsingScanLine(Bitmap1, Bitmap2: TBitmap): Boolean;
var
 i           : Integer;
 ScanBytes   : Integer;
begin
  Result:= (Bitmap1<>nil) and (Bitmap2<>nil);
  if not Result then exit;
  Result:=(bitmap1.Width=bitmap2.Width) and (bitmap1.Height=bitmap2.Height) and (bitmap1.PixelFormat=bitmap2.PixelFormat) ;

  if not Result then exit;

  ScanBytes := Abs(Integer(Bitmap1.Scanline[1]) - Integer(Bitmap1.Scanline[0]));
  for i:=0 to Bitmap1.Height-1 do
  Begin
    Result:=CompareMem(Bitmap1.ScanLine[i],Bitmap2.ScanLine[i],ScanBytes);
    if not Result then exit;
  End;

end;

再见。

答案 2 :(得分:0)

如果您需要准确答案,请不要。如果需要近似值,则可能需要选择像素。但是如果你想知道两个位图是否完全相同,你需要比较整个像素和像素格式数据。

相关问题