Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

ImageSearch Alternative


  • Please log in to reply
4 replies to this topic
Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

Due to a post I saw, I figured I'd properly post my working alternative function for ImageSearch, which uses the Gdip library.

Credits:
Tic, for creating the Gdip library.
MasterFocus, for creating Gdip_ImageSearch() and for assisting me with a bug in my function.

Gdip library required
This function requires both Gdip and Gdip_ImageSearch. For convenience, I've linked the file I use, which is Gdip_All.ahk with all necessary Gdip_ImageSearch functions appended.
Download: Gdip_All.ahk
 

/*
ImageSearchc - alternative to ImageSearch (uses GDI+; requires Gdip.ahk by Tic and Gdip_ImageSearch (put inside Gdip.ahk) by MasterFocus)
    Param1 - found x-coord
    Param2 - found y-coord
    Param3 - top x corner of area to search
    Param4 - top y corner of area to search
    Param5 - bottom x corner of area to search
    Param6 - bottom y corner of area to search
    Param7 - file of image to search for
    Param8 - variation (see doc for ImageSearch)
        Default: 0
    Param9 - transcolor (see doc for ImageSearch)
        Default: ""
    Param10 - relative (base coords off active window) (boolean)
        Default: 0
    Param11 - Search direction
        Vertical preference:
           1 = top->left->right->bottom
           2 = bottom->left->right->top
           3 = bottom->right->left->top
           4 = top->right->left->bottom
         Horizontal preference:
           5 = left->top->bottom->right
           6 = left->bottom->top->right
           7 = right->bottom->top->left
           8 = right->top->bottom->left
        Default: 5
    Param12 - save captured screen to active directory for debugging (boolean)
        Default: 0
*/
 
ImageSearchc(ByRef out1, ByRef out2, x1, y1, x2, y2, image, vari=0, trans="", rela=0, direction=5, debug=0){
    dxoff:=0
    dyoff:=0
    if(rela)
    {
        WinGetPos,xoff,yoff,woff,hoff,A
        x2=x2 > woff ? woff : x2
        y2=y2 > hoff ? hoff : y2
        x1+=xoff,x2+=xoff
        y1+=yoff,y2+=yoff
    }
    
    ptok:=Gdip_Startup()
    imageB:=Gdip_CreateBitmapFromFile(image)
    Scrn:=Gdip_Bitmapfromscreen(x1 "|" y1 "|" x2 - x1 "|" y2 - y1)
    if(debug)
        Gdip_SaveBitmapToFile(Scrn, a_now ".png")
    Errorlev:=Gdip_ImageSearch(Scrn,imageB,tempxy,0,0,0,0,vari,trans,direction)
    Gdip_DisposeImage(Scrn)
    Gdip_DisposeImage(imageB)
    Gdip_Shutdown(ptok)
    if(Errorlev > 0)
    {
        out:=StrSplit(tempxy,"`,")
        out1:=out[1] + x1 - dxoff
        out2:=out[2] + y1 - dyoff
        return % Errorlev
    }
    return 0
}

IMPORTANT - 


This function assumes the library has not been started. If you try loading in the library multiple times, you shouldn't have a problem, but I feel it's pertinent to mention this.


OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


DataLife
  • Members
  • 1022 posts
  • Last active: Nov 27 2015 01:09 AM
  • Joined: 27 Apr 2008
What are the benefits of using Gdip over ImageSearch?
Check out my scripts.  (MyIpChanger) (XPSnap) (SavePictureAs) All my scripts are tested on Windows 7, AutoHotkey 32 bit Ansi unless otherwise stated.

Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
For one it can be used on an inactive none top window
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

For one it can be used on an inactive none top window

Unfortunately, this does not do that. This takes relative shots by grabbing the active window's position and doing a coord-based BitmapFromScreen. In previous attempts at using the built-in method, it altered the picture, mainly the border, in a strange way.

The reason I wrote this function was to be able to grab a screenshot on a full-screened flash window. I could very well add another parameter to allow using the other method, though.


OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
@Masonjar

okay :) did not really read into your version.

It was just one of the benefits that have made me use GDIp for image-searching at times
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About