Jump to content

Transparent Bitmap using Bitmap Masking


Malkey
 Share

Recommended Posts

The $SRCAND raster operation, or ROP code for BitBlt() means to combine the bits using AND.

An explanation of the other ROP code, $SRCCOPY, $SRCINVERT, $PATINVERT, and $SRCPAINT can be found in the help file under _WinAPI_BitBlt().

For transparency, the idea is to combine the bits of the to created masks using $SRCAND and $SRCPAINT.

Color mask - Has black where transparency will be. All other colors remain untouched.

Black/White Mask - Has white where the transparency will be, all other colors appear Black.

The color to be manipulated (in this case for transparency) is selected when the image is copied to the GUI which has THE COLOR as its background color.

The background color of the GUI determines which color in the image is to become transparent

This is just a demo of the possibilities of bitmap masking.

The color used is the top left desktop pixel at (x, y) = (3, 3). This point being within the captured desktop image which is used for this demo.

Unfortunately, when saving the resultant partly transparent image, the background color of the display GUI is also saved - not the transparent parts.

#include <WinAPI.au3>
#include <ScreenCapture.au3>
#include <GDIPlus.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <misc.au3>

Global $iX = 0, $iY = 0, $iWidth = 600, $iHeight = 400, $iH, $iW

Global $iTransparentColor ; = 0x3A6EA5 ;0xFFFFFF ;
if Not IsNumber($iTransparentColor) Then $iTransparentColor = "0x" & Hex(PixelGetColor(3,3),6) ;GetPixel($hGUI2,3,3)
BitmapMasking(@DesktopDir & "\test.png", 0, 0, $iWidth, $iHeight)
;ShellExecute(@DesktopDir & "\test.png")

While GUIGetMsg() <> -3
    Sleep(10)
WEnd

Func BitmapMasking($sFileName = "", $iX=0, $iY=0 , $iWidth=10 , $iHeight=10 )
    Local $hWnd, $hDDC, $hCDC, $hBMP, $aIcon, $hIcon
    
    $iW = $iWidth 
    $iH = $iHeight
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDDC = _WinAPI_GetDC($hWnd)
    $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iW, $iH)
    _WinAPI_SelectObject($hCDC, $hBMP)
    
    ; Create GUI for Color Mask
    $GUICompDir = GUICreate("Color Mask", $iWidth, $iHeight, 0, 0)
    WinSetTrans("Color Mask", "", 255)
    GUISetBkColor($iTransparentColor, $GUICompDir) ;<=== TransparentColor ==
    GUISetState()   
    $hDDC1 = _WinAPI_GetDC($GUICompDir)
    $hCDC1 = _WinAPI_CreateCompatibleDC($hDDC1)
    $hBMP1 = _WinAPI_CreateCompatibleBitmap($hDDC1, $iW, $iH)
    _WinAPI_SelectObject($hCDC1, $hBMP1)
    
    ; Create GUI for B/W Mask
    $hGUI = GUICreate("B/W Mask", $iWidth, $iHeight, -1, @DesktopHeight / 2)
    ;WinSetTrans("B/W Mask", "", 254)
    GUISetState()
    $hDDC2 = _WinAPI_GetDC($hGUI)
    $hCDC2 = _WinAPI_CreateCompatibleDC($hDDC2)
    $hBMP2 = _WinAPI_CreateCompatibleBitmap($hDDC2, $iW, $iH)
    _WinAPI_SelectObject($hCDC2, $hBMP2)
    
    ; Create GUI
    $hGUIRes = GUICreate("Result", $iWidth, $iHeight, @DesktopWidth / 2, 10)
    ;WinSetTrans("Result", "", 254)
    ;GUISetBkColor(0x80FF80 , $hGUIRes)
    ;GUISetBkColor($GUI_BKCOLOR_TRANSPARENT , $hGUIRes)
    GUISetState()
    $hDDC3 = _WinAPI_GetDC($hGUIRes)
    $hCDC3 = _WinAPI_CreateCompatibleDC($hDDC3)
    $hBMP3 = _WinAPI_CreateCompatibleBitmap($hDDC3, $iW, $iH)
    _WinAPI_SelectObject($hCDC3, $hBMP3)
    
    ; A one bit per pixel bitmap 
    $hBitmapMask = _WinAPI_CreateBitmap($iW, $iH, 1, 1, 0)
    $hdcMem = _WinAPI_CreateCompatibleDC(0);
    _WinAPI_SelectObject($hdcMem, $hBitmapMask);

    ; The GUI associated with $hDDC1 has the background color set to the transparent color,
    ; and, $hDDC is the handle of the display device context for the captured desktop area.
    _WinAPI_BitBlt($hDDC1, 0, 0, $iW, $iH, $hDDC, $iX, $iY, $SRCCOPY)    ; Direct copy
    _WinAPI_BitBlt($hdcMem, 0, 0, $iW, $iH, $hDDC1, $iX, $iY, $SRCCOPY)  ; Direct copy
    
    ; Color mask - Has black where transparency will be 
    _WinAPI_BitBlt($hDDC1, 0, 0, $iW, $iH, $hdcMem, $iX, $iY, $SRCINVERT) ;XOR operator
    
    ; Black/White Mask - Has white where the transparency will be, all other colors appear black.
    _WinAPI_BitBlt($hDDC2, 0, 0, $iW, $iH, $hdcMem, $iX, $iY, $SRCINVERT) ; XOR operator    
    _WinAPI_BitBlt($hDDC2, 0, 0, $iW, $iH, $hDDC1, $iX, $iY, $PATINVERT)  ; XOR operator    

    ; Apply Masks
    _WinAPI_BitBlt($hDDC3, 0, 0, $iW, $iH, $hDDC2, $iX, $iY, $SRCAND)     ; AND operator
    _WinAPI_BitBlt($hDDC3, 0, 0, $iW, $iH, $hDDC1, $iX, $iY, $SRCPAINT)   ; OR operator
    
    ; Copy Dislpay DC ($hDDC3) to memory DC ($hCDC3) also known as $hBMP3
    _WinAPI_BitBlt($hCDC3, 0, 0, $iW, $iH, $hDDC3, $iX, $iY, $SRCCOPY)    ; Direct copy     
    
    _WinAPI_ReleaseDC($hWnd, $hDDC)
    _WinAPI_DeleteDC($hCDC)
    _WinAPI_ReleaseDC($GUICompDir, $hDDC1)
    _WinAPI_DeleteDC($hCDC1)
    _WinAPI_ReleaseDC($hGUI, $hDDC2)
    _WinAPI_DeleteDC($hCDC2)
    _WinAPI_ReleaseDC($hGUIRes, $hDDC3)
    _WinAPI_DeleteDC($hCDC3)
    _WinAPI_DeleteObject($hBMP)
    _WinAPI_DeleteObject($hBMP1)
    _WinAPI_DeleteObject($hBMP2)
    _WinAPI_DeleteObject($hBitmapMask)
    
    If $sFileName = "" Then Return $hBMP3
    _ScreenCapture_SaveImage($sFileName, $hBMP3)    
    _WinAPI_DeleteObject($hBMP3)
EndFunc   ;==>BitmapMasking

Someone might find a useful purpose for this.

Link to comment
Share on other sites

Hi,

Yep, the simplicity of your example helps me better understand the bitmap AND , XOR masks..

Which in turn helps me output *.ico files from any type of input image.

So I did find your example useful :P

Thank You

'N'

Cheers

Edited by smashly
Link to comment
Share on other sites

Hi,

Yep, the simplicity of your example helps me better understand the bitmap AND , XOR masks..

Which in turn helps me output *.ico files from any type of input image.

So I did find your example useful :D

Thank You

'N'

Cheers

:P:o:P

Is there a possibility you could show us how you've achieved this? :zorro:

My scripts:AppLauncherTRAY - Awesome app launcher that runs from the system tray NEW VERSION! | Run Length Encoding - VERY simple compression in pure autoit | Simple Minesweeper Game - Fun little game :)My website

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...