Sign in to follow this  
Followers 0
user2037

Creating Alpha Mask Using Difference Between Two Images

2 posts in this topic

In order to cope with custom-pointer images I'm trying to create an alpha mask by sampling a screen region with and without the pointer. Creating the mask of the unchanged pixels is not obvious to me. (Being new to GDI.)

Any ideas?

Share this post


Link to post
Share on other sites



You can blit one image on the other using the XOR operator:

#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>


Global $hGui = GUICreate("", 604, 202)
GUISetBkColor(0x0000FF)
Global $hDC = _WinAPI_GetDC($hGui)
Global $hCDC = _WinAPI_CreateCompatibleDC($hDC)
GUISetState()

Global $hBMP_1, $hBMP_2
_CreateGraphics($hBMP_1, $hBMP_2)

Local $hOBJ = _WinAPI_SelectObject($hCDC, $hBMP_1)
_WinAPI_BitBlt($hDC, 1, 1, 200, 200, $hCDC, 0, 0, $SRCCOPY)
_WinAPI_SelectObject($hCDC, $hBMP_2)
_WinAPI_BitBlt($hDC, 202, 1, 200, 200, $hCDC, 0, 0, $SRCCOPY)


Global $iMode = 0
Global $iTimer = TimerInit()
While GUIGetMsg() <> -3
    If TimerDiff($iTimer) > 500 Then
        $iTimer = TimerInit()
        Switch $iMode
            Case 1
                _WinAPI_SelectObject($hCDC, $hBMP_2)
                _WinAPI_BitBlt($hDC, 403, 1, 200, 200, $hCDC, 0, 0, $SRCCOPY)
                $iMode = 2
            Case 2
                _WinAPI_SelectObject($hCDC, $hBMP_1)
                _WinAPI_BitBlt($hDC, 403, 1, 200, 200, $hCDC, 0, 0, $SRCCOPY)
                _WinAPI_SelectObject($hCDC, $hBMP_2)
                _WinAPI_BitBlt($hDC, 403, 1, 200, 200, $hCDC, 0, 0, $SRCINVERT) ;<---- XOR
                $iMode = 0
            Case Else
                _WinAPI_SelectObject($hCDC, $hBMP_1)
                _WinAPI_BitBlt($hDC, 403, 1, 200, 200, $hCDC, 0, 0, $SRCCOPY)
                $iMode = 1
        EndSwitch
    EndIf
WEnd

_WinAPI_SelectObject($hCDC, $hOBJ)
_WinAPI_DeleteObject($hBMP_1)
_WinAPI_DeleteObject($hBMP_2)
_WinAPI_DeleteDC($hCDC)
_WinAPI_ReleaseDC($hGui, $hDC)









Func _CreateGraphics(ByRef $hBMP_1, ByRef $hBMP_2)
    _GDIPlus_Startup()

    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0(200, 200)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hContext, 2)
    _GDIPlus_GraphicsClear($hContext, 0xFFFFFFFF)
    Local $hPen = _GDIPlus_PenCreate(0xFF00FF00, 5)


    For $i = 1 To 10
        _GDIPlus_PenSetColor($hPen, BitOR(0xFF000000, BitShift(Random(0, 255, 1), -16), BitShift(Random(0, 255, 1), -8), Random(0, 255, 1)))
        _GDIPlus_GraphicsDrawLine($hContext, Random(0, 200), Random(0, 200), Random(0, 200), Random(0, 200), $hPen)
    Next
    $hBMP_1 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)

    For $i = 1 To 4
        _GDIPlus_PenSetColor($hPen, BitOR(0xFF000000, BitShift(Random(0, 255, 1), -16), BitShift(Random(0, 255, 1), -8), Random(0, 255, 1)))
        _GDIPlus_GraphicsDrawLine($hContext, Random(0, 200), Random(0, 200), Random(0, 200), Random(0, 200), $hPen)
    Next
    $hBMP_2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)


    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hContext)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
EndFunc   ;==>_CreateGraphics

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  
Followers 0