Jump to content

gdi plus help... why is this not working


Recommended Posts

#include <IE.au3>
#include <ScreenCapture.au3>
#include <GDIPlus.au3>
#Include <WinAPIEx.au3>



_GDIPlus_Startup()
    ; Capture 32 bit bitmap
    $hBitmap = _ScreenCapture_Capture("")
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

    ; Create 24 bit bitmap clone
    $iX = _GDIPlus_ImageGetWidth($hImage)
    $iY = _GDIPlus_ImageGetHeight($hImage)
    $hClone = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iX, $iY, $GDIP_PXF08INDEXED)
MsgBox('','','start',1)
$file=FileOpen("test.txt")
    for $y=int(@DesktopHeight*.15) to @DesktopHeight step 2
;~      MsgBox("","y",$y,1)
    for $x=0 to @DesktopWidth-Int(@DesktopWidth*.25) step 2

;~      MsgBox("","x",$x,1)
    $color=_GDIPlus_BitmapGetPixel($hClone,$x,$y)
    FileWriteLine($file,"x" & $x & "y" & $y & "color" & $color)
    if $color="0xff0000" Then
msgbox("","","x" & $x & "y" & $y)
        ExitLoop
        EndIf
    Next
    if $color=0xff0000 Then
        ExitLoop
        EndIf
    Next
FileClose($file)

    ; Save bitmap to file
    _GDIPlus_ImageSaveToFile($hClone, @MyDocumentsDir & "\GDIPlus_Image.bmp")

    ; Clean up resources
    _GDIPlus_ImageDispose($hClone)
    _GDIPlus_ImageDispose($hImage)
    _WinAPI_DeleteObject($hBitmap)

    ; Shut down GDI+ library
    _GDIPlus_Shutdown()

    ShellExecute(@MyDocumentsDir & "\GDIPlus_Image.bmp")

it does everything except actually find the color and I tried to get it to log every color it finds  and it doesn't do that but it does infact have a bitmap clone  created in memory and it saves it and displays it just fine afterwards.  so im assuming the issue is in the 2x for loops...

 

any help would be great thanks

Link to comment
Share on other sites

To answer your question, you need to include the alpha channel. _GDIPlus_BitmapGetPixel will return the ARGB value of the pixel (Alpha, aka transparency, red, green, and blue). So you need 0xFFFF0000 for your search color.

I tried doing this same thing a long time ago, searching for a color in memory, and did the same thing and it took soooooo long. Since then I discovered LockBits (and BitBlt but that's HBitmap). Anyway, this will do what you want a LOT faster. And if you remove the part where you write to a file it will do it even faster. (1920x1080 desktop, approximately 17seconds to go through every pixel while writing to the file, approximately 2 seconds to go through every pixel not writing to the file).

#include <ScreenCapture.au3>
#include <GDIPlus.au3>
#include <WinApi.au3>

_GDIPlus_Startup()
Global $hHBitmap = _ScreenCapture_Capture("")
Global $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
Global $iW = _GDIPlus_ImageGetWidth($hBitmap)
Global $iH = _GDIPlus_ImageGetHeight($hBitmap)
Global $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32PARGB) ;locks a portion of a bitmap for reading and writing. More info at http://msdn.microsoft.com/en-us/library/windows/desktop/ms536298(v=vs.85).aspx
Global $iScan0 = DllStructGetData($tBitmapData, "Scan0")
Global $iSearchPixel = Int(0xFFFF0000)
Global $tPixel = DllStructCreate("int[" & $iW * $iH & "];", $iScan0)
Global $iPixel, $iRowOffset
Global $iFoundX, $iFoundY
Global $hFile = FileOpen(@ScriptDir & "\GDI Pixel Search.txt", 2)
Global $iTimer = TimerInit()

For $iY = 0 To $iH - 1
    $iRowOffset = $iY * $iW + 1
    For $iX = 0 To $iW - 1 ;get each pixel in each line and row
        $iPixel = DllStructGetData($tPixel, 1, $iRowOffset + $iX) ;get pixel color
        FileWrite($hFile, $iX & ", " & $iY & " = 0x" & Hex($iPixel) & @CRLF)
        If $iPixel = $iSearchPixel Then
            $iFoundX = $iX
            $iFoundY = $iY
        EndIf
    Next
Next

ConsoleWrite(TimerDiff($iTimer) / 1000 & @CRLF)
FileClose($hFile)
_GDIPlus_BitmapUnlockBits($hBitmap, $tBitmapData)

; Save bitmap to file
_GDIPlus_ImageSaveToFile($hBitmap, @MyDocumentsDir & "\GDIPlus_Image.bmp")

; Clean up resources
_GDIPlus_BitmapDispose($hBitmap)
_WinAPI_DeleteObject($hHBitmap)

; Shut down GDI+ library
_GDIPlus_Shutdown()

ShellExecute(@MyDocumentsDir & "\GDIPlus_Image.bmp")

It would be even faster if you made a dll out of it and used a dllcall, like the advanced pixel dll on the forum. I didn't like any of the functions from that dll so I've never used it.

Link to comment
Share on other sites

o wow ty so much.  I too have been through the other available image searching methods on the site and i had got this close to working before and one reason or another I gave up and i'm working on somthing else and decided to give this another go.  But again ty i really do appreciate it

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...