Jump to content
Sign in to follow this  

Need GDI graphics cleared after BitmapSetPixel()

Recommended Posts


I'm really new to working with the GDI API. What I want my script to do is:

  1. get a screenshot of a 100x100 region
  2. go through a list of captured colors in this region
  3. highlight them in red
  4. prompt if I want to continue to another captured color
    1. somehow make the red parts of the region go back to normal/reset/clear/revert to original screenshot
    2. Repeat Steps 2 to 4

Here's my work on how far I got:

#include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <GDIPlus.au3>
#include <Array.au3>

Global $g_hGUI, $g_hGfxCtxt, $g_hBitmap, $g_hBMP, $g_hGraphics
HotKeySet('{F1}', 'Example')

While 1

Func Example()
    AutoItSetOption("GUIOnEventMode", 1)
    Opt("MouseCoordMode", 0)

    Local $iW = 50, $iH = 50
    Local Const $iWidth = $iW * 2, $iHeight = $iH * 2, $iBgColor = 0x303030 ;$iBGColor format RRGGBB

    ;   create a GDI bitmap by capturing a confined region with the mouse cursor as the center
    $myMouseCoords = MouseGetPos()
    Local $hHBmp = _ScreenCapture_Capture("", $myMouseCoords[0] - $iW, $myMouseCoords[1] - $iH, $myMouseCoords[0] + $iW, $myMouseCoords[1] + $iH, False)

    _GDIPlus_Startup() ;initialize GDI+

    ;   create a GUI with the dimensions fitting the captured region for previewing screenshot
    $g_hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight)
    GUISetBkColor($iBgColor, $g_hGUI) ;set GUI background color

    #Region     GDI+ setup
    $g_hGraphics = _GDIPlus_GraphicsCreateFromHWND($g_hGUI) ;create a graphics object from a window handle
    $g_hBitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $g_hGraphics) ;   Scaling: Magnify the captured screen by /2, zoom out by *2
    $g_hGfxCtxt = _GDIPlus_ImageGetGraphicsContext($g_hBitmap)
    $g_hBMP = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore
    Local $iX = 0.0, $iY = 0.0 ;define start coordinate
    #EndRegion GDI+ setup

    Local $aColorCoords[$iHeight * $iWidth][2], $pixelCounter
    For $iRow = 0 To $iHeight - 1
        For $iCol = 0 To $iWidth - 1
            $iColor = _GDIPlus_BitmapGetPixel($g_hBMP, $iCol, $iRow) ;get current pixel color
            $aColorCoords[$pixelCounter][0] = $iColor
            $aColorCoords[$pixelCounter][1] = $iCol & "," & $iRow
            $pixelCounter += 1

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    $aUniqueColors = _ArrayUnique($aColorCoords, 0, 0, 0, $ARRAYUNIQUE_NOCOUNT) ;   Creates an array of unique colors from the whole capture
    _ArrayColInsert($aUniqueColors, 1)

    #Region     CORE PROBLEM
    For $findUnique = 0 To UBound($aUniqueColors) - 1 ; Find and stores the total of every color captured
        _GDIPlus_GraphicsClear($g_hGfxCtxt, 0xFF000000 + $iBgColor) ;   clear bitmap for repaint
        $aColorIndexes = _ArrayFindAll($aColorCoords, $aUniqueColors[$findUnique][0])
        $aUniqueColors[$findUnique][1] = UBound($aColorIndexes)
        $totalQueries = UBound($aUniqueColors)
        For $getIndex = 0 To UBound($aColorIndexes) - 1
            $sThisCoordSet = $aColorCoords[$aColorIndexes[$getIndex]][1]
            $iCommaPosition = StringInStr($sThisCoordSet, ',')
            $iColorCoordX = StringMid($sThisCoordSet, 1, $iCommaPosition - 1) ; The index yields a string of the coords; they need to be parsed to integers
            $iColorCoordY = StringMid($sThisCoordSet, $iCommaPosition + 1) ;    +1 ignores the comma
            p('$iColorCoordX', $iColorCoordX)
            p('$iColorCoordY', $iColorCoordY)
            _GDIPlus_BitmapSetPixel($g_hBMP, $iColorCoordX, $iColorCoordY, 0xFFFF0000)
        _GDIPlus_GraphicsDrawImage($g_hGfxCtxt, $g_hBMP, $iX, $iY) ;    draw bitmap to backbuffer
        _GDIPlus_GraphicsDrawImageRect($g_hGraphics, $g_hBitmap, 0, 0, $iWidth, $iHeight) ;copy drawn bitmap to graphics handle (GUI)
    #EndRegion CORE PROBLEM
EndFunc   ;==>Example

Func _Exit()
    ;cleanup GDI+ resources
EndFunc   ;==>_Exit

_GDIPLUS_GraphicsClear() doesn't work. I tried it on $g_hGraphics, $g_hBitmap, $g_hGfxCtxt, and $g_hBMP which are my graphics objects located in #Region GDI+ setup. My usage is under the #Region CORE PROBLEM. I wonder what I'm misunderstanding here.

Share this post

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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.