Jump to content

BMP to PNG with transparent color

Recommended Posts


I'd like to load a BMP file then change chosen color (0xFF00FF) to transparent and save it as PNG file. I'm using a code from other thread. I've modified it a little, but it still doesn't work.

My program displays image with transparent color correctly, however saves a black image file.

#include <Constants.au3>
#include <WinAPIEx.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>

Global Const $STM_SETIMAGE = 0x0172
Global Const $STM_GETIMAGE = 0x0173

$hForm = GUICreate('MyGUI', 400, 400)
$Pic = GUICtrlCreatePic('', 50, 50, 144, 87)
$hPic = GUICtrlGetHandle(-1)


$hDstDC = _WinAPI_CreateCompatibleDC(0)
$hBitmap = _WinAPI_CreateSolidBitmap(0, _WinAPI_GetSysColor($COLOR_3DFACE), 144, 87, 0)
$hDstSv = _WinAPI_SelectObject($hDstDC, $hBitmap)
$hSrcDC = _WinAPI_CreateCompatibleDC(0)
$hImg = _WinAPI_LoadImage(0, @ScriptDir & '\piclvq.bmp', $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
$hSrcSv = _WinAPI_SelectObject($hSrcDC, $hImg)
_WinAPI_TransparentBlt($hDstDC, 0, 0, 144, 87, $hSrcDC, 0, 0, 144, 87, 0xFF00FF)

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hForm)
$hImage = _GDIPlus_BitmapCreateFromGraphics(144, 87, $hGraphic)

; Save resultant image
_GDIPlus_ImageSaveToFile($hImage, "My_transparent_image.png")

_WinAPI_SelectObject($hDstDC, $hDstSv)
_WinAPI_SelectObject($hSrcDC, $hSrcSv)

_WinAPI_DeleteObject(_SendMessage($hPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hBitmap))
$hPic = _SendMessage($hPic, $STM_GETIMAGE)
If $hPic <> $hBitmap Then


Until GUIGetMsg() = -3


Edited by Shanheavel

Share this post

Link to post
Share on other sites

Yeah, it works! You saved me a lot of time. I've just replaced screen capturing with making bitmap from a file.

Here is full working code:

#include <ScreenCapture.au3>

Global $iWidth = 144, $iHeight = 87
Global $hBitmap_GDIPlus = _GDIPlus_BitmapCreateFromFile("piclvq.bmp")
Global $hBitmap_Result = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
Global $hBitmap_Result_Ctxt = _GDIPlus_ImageGetGraphicsContext($hBitmap_Result)

Global $aRemapTable[2][2]
$aRemapTable[0][0] = 1
$aRemapTable[1][0] = 0xFFFF00FF ;Farbe, die Transparent gemacht werden soll

Global $hIA = _GDIPlus_ImageAttributesCreate()
_GDIPlus_ImageAttributesSetRemapTable($hIA, 1, True, $aRemapTable)
_GDIPlus_GraphicsDrawImageRectRect($hBitmap_Result_Ctxt, $hBitmap_GDIPlus, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA)
_GDIPlus_ImageSaveToFile($hBitmap_Result, @ScriptDir & "\Result.png")


ShellExecute(@ScriptDir & "\Result.png")

Func _GDIPlus_ImageAttributesSetRemapTable($hImageAttributes, $iColorAdjustType = 0, $fEnable = False, $aColorMap = 0)
    Local $iI, $iCount, $tColorMap, $aResult
    If IsArray($aColorMap) And UBound($aColorMap) > 1 Then
        $iCount = $aColorMap[0][0]
        $tColorMap = DllStructCreate("uint ColorMap[" & $iCount * 2 & "]")
        For $iI = 1 To $iCount
            $tColorMap.ColorMap((2 * $iI - 1)) = $aColorMap[$iI][0]
            $tColorMap.ColorMap((2 * $iI)) = $aColorMap[$iI][1]
        $aResult = DllCall($__g_hGDIPDll, "int", "GdipSetImageAttributesRemapTable", "handle", $hImageAttributes, "int", $iColorAdjustType, "int", $fEnable, "int", $iCount, "struct*", $tColorMap)
        If @error Then Return SetError(@error, @extended, False)
        If $aResult[0] Then Return SetError(10, $aResult[0], False)
        Return True
    Return SetError(11, 0, False)
EndFunc   ;==>_GDIPlus_ImageAttributesSetRemapTable


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

  • Similar Content

    • Sven-Seyfert
      By Sven-Seyfert
      Hi Community,

      I'm looking for a way to do a Video Overlay GUI or something like that. The idea is to create a GUI which plays a video loop (with transparency/alpha channel) in front of an other GUI. Before you asking why - because I don't believe that GDIPlus can do it out of the box. My skillset for that kind of graphical things isn't good enough to do that, but here are some specialist like @UEZ maybe who can help.

      Example alpha channel video (visualized as animated *.gif):

      I tried to do the light rays effect directly with GDIPlus, but honestly that's a bit too difficult for me. I would be very glad and grateful if there are some suggestions, ideas or recommendations.

      Code for the Video play:
      Example video "End.mpeg":
      The next challenge is that the overlay GUI should be not clickable. If I hover over the overlay area, I want to have the possibility to control the GUI or what ever, in the background. But if there is any chance to make it with GDIPlus as a Video Overlay for light rays, I would prefer that approach instead of my crazy work-around idea.

      Thanks for any suggestion - I'm grateful!
    • c.haslam
      By c.haslam
      I had thought that _GDIPlus_ImageClone($hImage) removes all property items, but I now know that it copies property items.
      What is the easiest way of copying an image that has property items to another image that does not have property items?
    • aiter
      By aiter
      I am trying to get an image showing through a edit box.  I am only successful in making the edit box totally transparent
      #include-once #include <GUIConstants.au3> #include <GDIPlus.au3> #include <WinAPISys.au3> #include <colorconstants.au3> ;WS_EX_TRANSPARENT $gui = GUICreate("", 1000, 800, -1, -1, -1 , $WS_EX_LAYERED) ; use layered to get _winapi_setlay... to work $pic = GUICtrlCreatePic("c:\Program Files (x86)\AutoIt3\Examples\GUI\Merlin.gif", 0,0,1000, 800) GUICtrlSetState(-1, $GUI_DISABLE) $edit = GUICtrlCreateEdit("First line" & @CRLF, 176, 32,200,600) GUICtrlSetBkColor(-1,$COLOR_YELLOW) _WinAPI_SetLayeredWindowAttributes($gui,$COLOR_YELLOW,199) ; 199 is alpha (transparency level) GUISetState(@SW_SHOW,$gui) While 1 $msg = GuiGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop Case Else EndSelect WEnd Exit Func Terminate() exit(0) EndFunc So I am making the edit box's background yellow then using the _WINAPI_SetLayeredWIndowAttributes command to make the yellow disappear (which it does), but the alpha level is supposed to give a bit of opaqueness to it, but its not, just making it totally transparent.  The alpha level is in fact affecting the window itself and not the edit box. I only want the edit box to be partially transparent.
      Help appreciated.
    • ISI360
      By ISI360
      I am searching for a way to display a transparent image (or Icon) over an Button. And the image should always stay on top.

      Here is what i have so far:
      At script start the picture is over the button..but if you move the mouse over the button it overlaps the image.
      And if i add $GUI_ONTOP to the picture, the transparency is gone

      Maybe someone has an idea..

      Thanks in advance!
      #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WinAPIIcons.au3> #include <WinAPIShellEx.au3> GUICreate("GUI", 300, 300) $hbutton = GUICtrlCreateButton("This is a button", 20, 40, 150, 30,$WS_CLIPSIBLINGS) $hpic = GUICtrlCreatePic("", 10, 10, 128, 128) _SetIconAlpha($hpic,"shell32.dll", 12,128,128) GUICtrlSetState(-1, $GUI_DISABLE) ;~ GUICtrlSetState(-1,$GUI_ONTOP) ;If you uncomment this..the picture is on top...but the transparency is gone :( GUISetState() While 1 $Msg = GUIGetMsg() If $Msg = $GUI_EVENT_CLOSE Then ExitLoop WEnd Func _SetIconAlpha($hWnd, $sIcon, $iIndex, $iWidth, $iHeight) If Not IsHWnd($hWnd) Then $hWnd = GUICtrlGetHandle($hWnd) If $hWnd = 0 Then Return SetError(1, 0, 0) EndIf EndIf If $iIndex <> 0 Then $iIndex = $iIndex - 1 Local $hIcon = _WinAPI_ShellExtractIcon($sIcon, $iIndex, $iWidth, $iHeight) If $hIcon = 0 Then Return SetError(1, 0, 0) EndIf Local $hBitmap, $hObj, $hDC, $hMem, $hSv $hDC = _WinAPI_GetDC($hWnd) $hMem = _WinAPI_CreateCompatibleDC($hDC) $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $iWidth, $iHeight) $hSv = _WinAPI_SelectObject($hMem, $hBitmap) _WinAPI_DrawIconEx($hMem, 0, 0, $hIcon, $iWidth, $iHeight, 0, 0, 2) _WinAPI_ReleaseDC($hWnd, $hDC) _WinAPI_SelectObject($hMem, $hSv) _WinAPI_DeleteDC($hMem) _WinAPI_DestroyIcon($hIcon) _WinAPI_DeleteObject(_SendMessage($hWnd, 0x0172, 0, 0)) _SendMessage($hWnd, 0x0172, 0, $hBitmap) $hObj = _SendMessage($hWnd, 0x0173) If $hObj <> $hBitmap Then _WinAPI_DeleteObject($hBitmap) EndIf Return 1 EndFunc ;==>_SetIconAlpha