Jump to content

problem with comparing Image

Recommended Posts

hello guys..

I am new to Autoit (Started last week) and I am trying to write an script that need to compare an screen capture to a pre-saved image, if they are the same, then continue to next part and so on...

I found below code in the forum (thanks to monoceres who wrote it) and it work fine if I load both image from hard but it crash if I pass _ScreenCapture_Capture("" ,795,218,836,258) to its CompareBitmaps function.

so I tried to pass _GDIPlus_BitmapCreateFromHBITMAP( _ScreenCapture_Capture("" ,795,218,836,258)) and it always return false even if the image were the same. I am so confused.

any help would be appreciate

#include <GDIPlus.au3>
#include <screencapture.au3>

$fname1=FileOpenDialog("First image","","All images(*.bmp;*.jpg;*.png;)")
If $fname1="" Then Exit
;~ $fname2=FileOpenDialog("Second image image","","All images(*.bmp;*.jpg;*.png;)") ;commented from riginal code
;~ If $fname2="" Then Exit    ;commented from riginal code

$bm1 = _GDIPlus_ImageLoadFromFile($fname1)
;~ $bm2 = _GDIPlus_ImageLoadFromFile($fname2) ;commented from riginal code

$bm2 = _GDIPlus_BitmapCreateFromHBITMAP(_ScreenCapture_Capture("" ,795,218,836,258)) ;I add this and its not working
MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2))

Func CompareBitmaps($bm1, $bm2)

    $Bm1W = _GDIPlus_ImageGetWidth($bm1)
    $Bm1H = _GDIPlus_ImageGetHeight($bm1)
    $BitmapData1 = _GDIPlus_BitmapLockBits($bm1, 0, 0, $Bm1W, $Bm1H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData1, "Stride")
    $Scan0 = DllStructGetData($BitmapData1, "Scan0")

    $ptr1 = $Scan0
    $size1 = ($Bm1H - 1) * $Stride + ($Bm1W - 1) * 4

    $Bm2W = _GDIPlus_ImageGetWidth($bm2)
    $Bm2H = _GDIPlus_ImageGetHeight($bm2)
    $BitmapData2 = _GDIPlus_BitmapLockBits($bm2, 0, 0, $Bm2W, $Bm2H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData2, "Stride")
    $Scan0 = DllStructGetData($BitmapData2, "Scan0")

    $ptr2 = $Scan0
    $size2 = ($Bm2H - 1) * $Stride + ($Bm2W - 1) * 4

    $smallest = $size1
    If $size2 < $smallest Then $smallest = $size2
    $call = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)

    _GDIPlus_BitmapUnlockBits($bm1, $BitmapData1)
    _GDIPlus_BitmapUnlockBits($bm2, $BitmapData2)

    Return ($call[0]=0)

EndFunc  ;==>CompareBitmaps


Link to post
Share on other sites

I am going to run the final script multiple time (like 50 running process at the same time) and they all get in a line for doing this comparison ( one after another not simultaneously).

so I am worried about latency of writing and deleting to hard. it may slow the whole job. (each running script do this comparison 5 time in lifetime before it restart.

there should be a way to fix this.. I think there must be.. or may be I am wrong?

Link to post
Share on other sites

Try this instead:


#include <Screencapture.au3>

Global Const $hGDIBitmap1 = _ScreenCapture_Capture("", 0, 0, 199, 199)
Global Const $hGDIBitmap2 = _ScreenCapture_Capture("", 0, 0, 199, 199)
;~ Global Const $hGDIBitmap2 = _ScreenCapture_Capture("", 0, 10, 199, 209)

Global Const $hBitmap1 = _GDIPlus_BitmapCreateFromHBITMAP($hGDIBitmap1)
Global Const $hBitmap2 = _GDIPlus_BitmapCreateFromHBITMAP($hGDIBitmap2)

MsgBox(0, "Test", "Equal? -> " & ASM_BitCompareBitmapsMMX($hBitmap1, $hBitmap2))


Func ASM_BitCompareBitmapsMMX($hBitmap1, $hBitmap2) ;coded by UEZ build 2016-02-08
    Local $aDim1 = _GDIPlus_ImageGetDimension($hBitmap1), $aDim2 = _GDIPlus_ImageGetDimension($hBitmap2)
    If BitOR($aDim1[0] <> $aDim2[0], $aDim1[1] <> $aDim2[1]) Then Return False

    Local $tBitmapData1 = _GDIPlus_BitmapLockBits($hBitmap1, 0, 0, $aDim1[0], $aDim1[1], $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    Local $pScan1 = $tBitmapData1.Scan0
    Local $iStride1 = $tBitmapData1.Stride * 4
    Local $tPixelData1 = DllStructCreate("dword[" & Abs($iStride1 * $aDim1[1]) & "]", $pScan1)

    Local $tBitmapData2 = _GDIPlus_BitmapLockBits($hBitmap2, 0, 0, $aDim2[0], $aDim2[1], $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    Local $pScan2 = $tBitmapData2.Scan0
    Local $tPixelData2 = DllStructCreate("dword[" & Abs($iStride1 * $aDim1[1])  & "]", $pScan2)

    Local $tCodeBuffer = DllStructCreate("byte ASM[55]")
    $tCodeBuffer.ASM = "0x8B7424048B7C24088B4C240CB801000000F30F6F0EF30F6F17660FEFCA660F3817C9750D83C61083C71083E90477E2EB05B800000000C3"

    Local $aRet = DllCall("user32.dll", "uint", "CallWindowProcW", "ptr", DllStructGetPtr($tCodeBuffer), _
                          "ptr", DllStructGetPtr($tPixelData1), _
                          "ptr", DllStructGetPtr($tPixelData2), _
                          "uint", $aDim1[0] * $aDim1[1], _
                          "int", Null)

    _GDIPlus_BitmapUnlockBits($hBitmap1, $tBitmapData1)
    _GDIPlus_BitmapUnlockBits($hBitmap2, $tBitmapData2)

    Return $aRet[0]


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...