Jump to content

Replace a Color in an Image to transparent

Recommended Posts

Hey Guys!

I am currently trying to automate making icons for my small fan made game. Basically i just take a screenshot of a 3D Model in 32x32 and remove the BG color via Photoshop, then saving it as .png.
Atm my script creates a rectangle to visually show me the area of the screenshot and hides the rectangle once it takes a screenshot:

$Rectangle = GUICreate("Test", 32, 32, 1358, 438,$WS_POPUP,$WS_EX_TOPMOST)
WinSetTrans($Rectangle, "", 150)

HotKeySet("{DOWN}" , "close")
HotKeySet("{UP}" , "TakeScreenshot")

Global $funcs = False

While 1
    If $funcs = True Then
        ToolTip("Cheeeseee" , 0 , 30)
        GUISetState(@SW_HIDE, $Rectangle);Hide rectangle
        _ScreenCapture_Capture("C:\Users\PAT\Pictures\Item" & "\Test.jpg", 1358, 438, 1390, 470);Take Screenshot and save the Picture
        GUISetState(@SW_SHOW, $Rectangle);Unhide rectangle
        $funcs = False

Func close()
    ToolTip("Closing..." , 0 , 30)

Func TakeScreenshot()
   If $funcs = False Then
      $funcs = True
      $funcs = False
      ToolTip("Ready!" , 0 , 30)


The Screenshot it takes looks like this:

But i want it to look like this(transparent background):

How would you guys do it? I'm pretty new to GDI and graphics related stuff in AutoIT so i have no clue what to do 😕

Any help would be really appreciated!

Edit: Alright i found a solution! :)
I've used this script from @UEZ to make it work like intended. Thanks to UEZ! ♥


#include <ScreenCapture.au3>

Global $iWidth = 400, $iHeight = 400
Global $hBitmap_GDI = _ScreenCapture_Capture("", 0, 0, $iWidth - 1, $iHeight - 1, 0)
Global $hBitmap_GDIPlus = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap_GDI)
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] = 0xFFFFFFFF ;Farbe, die Transparent gemacht werden soll
$aRemapTable[1][1] = 0x08000000 ;Semi Transparenz - Format 0xAARRGGBB

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


Edited by Sebiix3
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

  • Recently Browsing   0 members

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