Jump to content

Screenshot to .png with Alpha/Transparency channel by a given color or color range. - (Moved)


Go to solution Solved by paw,

Recommended Posts

Hello ladys and gents, hello forum. How to:

1. Take a screenshot of an area/fullscreen
2. Make a color or color range transparent.
3. Save to *.png with alpha channel.

Yeah... thats actually it.
But im unable to figure it out for unknown reasons... lol.
My NOT working code...

DllCall("User32.dll", "bool", "SetProcessDPIAware") ; Get Real Screen Resolution because 4k

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

_GDIPlus_Startup()

WinActivate("[CLASS:MozillaWindowClass]") ; Just Firefox with a white website
Sleep(500) ; Adjust the sleep time as necessary

Local $hGDIBmap = _ScreenCapture_Capture("", 0, 0, 500, 500) ; Create a GDI bitmap
Local $hGDIPlusBmp = _GDIPlus_BitmapCreateFromHBITMAP($hGDIBmap) ; Convert GDI bitmap to GDI+ bitmap
_WinAPI_DeleteObject($hGDIBmap) ; Release GDI bitmap resource

; Get image dimensions
$iWidth = _GDIPlus_ImageGetWidth($hGDIPlusBmp)
$iHeight = _GDIPlus_ImageGetHeight($hGDIPlusBmp)

; Create a new GDI+ bitmap with alpha channel support
$hGDIPlusBmpNew = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)

; Create Graphics from the new bitmap
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hGDIPlusBmpNew)

; Draw the original bitmap onto the new one, so we keep the original content
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hGDIPlusBmp, 0, 0, $iWidth, $iHeight)

; Now, modify the new bitmap to set white pixels to transparent
For $y = 0 To $iHeight - 1
    For $x = 0 To $iWidth - 1
        ; Get the current pixel's color from the original image
        $iARGB = _GDIPlus_BitmapGetPixel($hGDIPlusBmp, $x, $y)

        ; Check if the pixel is pure white (0xFFFFFFFF)
        If $iARGB = 0xFFFFFFFF Then
            ; Set the pixel to transparent in the new bitmap
            _GDIPlus_BitmapSetPixel($hGDIPlusBmpNew, $x, $y, 0x00000000)
        EndIf
    Next
Next

; Save the modified image as PNG with transparency
_GDIPlus_ImageSaveToFile($hGDIPlusBmpNew, @ScriptDir & "\modified_screenshot.png")

; Release resources
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_ImageDispose($hGDIPlusBmp)
_GDIPlus_ImageDispose($hGDIPlusBmpNew)
_GDIPlus_Shutdown()


It actually works, but there is no alpha in the saved image for some reason..
Thanks!!
 

Edited by Blaxxun
Link to comment
Share on other sites

  • Moderators

Moved to the appropriate forum.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Solution
DllCall("User32.dll", "bool", "SetProcessDPIAware") ; Get Real Screen Resolution because 4k

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

_GDIPlus_Startup()

Global $g_hWnd = WinActivate("[CLASS:MozillaWindowClass]") ; Just Firefox with a white website
Sleep(500) ; Adjust the sleep time as necessary
Global $aWinPos = WinGetPos($g_hWnd)
Local $hGDIBmap = _ScreenCapture_Capture("", $aWinPos[0], $aWinPos[1], $aWinPos[0] + $aWinPos[2], $aWinPos[1] + $aWinPos[3]) ; Create a GDI bitmap
Local $hGDIPlusBmp = _GDIPlus_BitmapCreateFromHBITMAP($hGDIBmap) ; Convert GDI bitmap to GDI+ bitmap
_WinAPI_DeleteObject($hGDIBmap) ; Release GDI bitmap resource

; Get image dimensions
$iWidth = _GDIPlus_ImageGetWidth($hGDIPlusBmp)
$iHeight = _GDIPlus_ImageGetHeight($hGDIPlusBmp)

; Create a new GDI+ bitmap with alpha channel support
$hGDIPlusBmpNew = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)

; Create Graphics from the new bitmap
$hGraphics = _GDIPlus_ImageGetGraphicsContext($hGDIPlusBmpNew)

$hIA = _GDIPlus_ImageAttributesCreate()
Global $aRemapTable[2][2]
$aRemapTable[0][0] = 1
$aRemapTable[1][0] = 0xFFFFFFFF
$aRemapTable[1][1] = 0x00000000
_GDIPlus_ImageAttributesSetRemapTable($hIA, $aRemapTable)

_GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hGDIPlusBmp, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA)

_GDIPlus_ImageAttributesDispose($hIA)
; Save the modified image as PNG with transparency
_GDIPlus_ImageSaveToFile($hGDIPlusBmpNew, @ScriptDir & "\modified_screenshot.png")

; Release resources
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_ImageDispose($hGDIPlusBmp)
_GDIPlus_ImageDispose($hGDIPlusBmpNew)
_GDIPlus_Shutdown()

works for me

Link to comment
Share on other sites

On 4/2/2024 at 10:50 PM, UEZ said:

Did you check if captured image has really $iARGB = 0xFFFFFFFF pixels?

@UEZ Hello, thanks for your help.
No, I did not check if the captured image has an Alpha channel. I guess that _ScreenCapture_Capture() is not able to capture an Alpha channel.
The docs don't say anything about this and I did not look into the function.
But i think i know what you mean. Because it could also be RGBA or other combinations.
I assumed it will only be RGB and that the Alpha is created in the next step.

@paw Thank you! Yes, it also works on my end.
It is also WAY faster this way instead of iterating through every single pixel...
I don't know how this works yet because I have to look into

_GDIPlus_ImageAttributesCreate

first.
Thank you! 🙂

Link to comment
Share on other sites

Alpha channel is always calculated by the pixel, the underlying pixel and the alpha channel value. Your screenshot will never have an alpha channel although the displayed graphic has an alpha channel.

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Yes. This is what I thought too.
Screenshot just takes the pixels as they are on screen in RGB values.
This is why the Alpha is calculated after the screenshot is taken.
In my/this case the color white.
 

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