Blaxxun Posted April 2, 2024 Posted April 2, 2024 (edited) 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... expandcollapse popupDllCall("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 April 2, 2024 by Blaxxun
Moderators Melba23 Posted April 2, 2024 Moderators Posted April 2, 2024 Moved to the appropriate forum. Moderation Team Blaxxun 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
UEZ Posted April 2, 2024 Posted April 2, 2024 Did you check if captured image has really $iARGB = 0xFFFFFFFF pixels? 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Solution paw Posted April 2, 2024 Solution Posted April 2, 2024 expandcollapse popupDllCall("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
Blaxxun Posted April 4, 2024 Author Posted April 4, 2024 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! 🙂
UEZ Posted April 4, 2024 Posted April 4, 2024 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
Blaxxun Posted April 4, 2024 Author Posted April 4, 2024 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now