Hello everyone.
I recently had to capture some program windows for a tutorial and I wanted to use a dark background in PowerPoint but also wanted to print it with white background. You know the problem: capture just the client area or deal with semitransparent borders and shadows?
Then I remembered and interesting example in the ImageMagick documentation (
"Background Removal using Two Backgrounds") and decided to wrap it with AutoHotkey. The script below is the result.
Putting it plainly, the script hides the desktop and takes two screenshots: one with a black background and another one with a white one. Then ImageMagick compares both images and builds an image with alpha channel (variable transparency) from them. Something like this:

+

=
Notes
- Requires ImageMagick (duh)
- Tested only with AutoHotkey_L in Windows 7 x64 with Aero enabled and photographic wallpapers
- You may need to adapt the values in the initial code block ("configuration")
Code:
; configuration
Delay = 100
StartButtonName = Start
ImageMagickPath = %A_ProgramFiles%\ImageMagick
; backup initial status: wallpaper, background color and desktop icon visibility
RegRead, WallpaperPath, HKEY_CURRENT_USER, Control Panel\Desktop, Wallpaper
SplitPath, WallpaperPath, WallpaperFilename
WallpaperBackupPath = %A_ScriptDir%\%WallpaperFilename%
FileCopy, %WallpaperPath%, %WallpaperBackupPath%
BackgroundColorBackup := DllCall("GetSysColor", Int, 1) ; see http://msdn.microsoft.com/ms724371.aspx
RegRead, HideIcons, HKEY_CURRENT_USER, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, HideIcons
OnExit, CleanUp
; Win+PrtScr hotkey
#PrintScreen::
; hide desktop icons if visible
If (HideIcons = 0) {
ControlGet, HWND, Hwnd,, SysListView321, ahk_class Progman
If HWND =
ControlGet, HWND, Hwnd,, SysListView321, ahk_class WorkerW
WinHide, ahk_id %HWND%
}
; hide desktop taskbar and Start button (locale dependant)
WinHide ahk_class Shell_TrayWnd
WinHide %StartButtonName% ahk_class Button
; black background capture
DllCall("SetSysColors", Int, 1, IntP, 1, UIntP, 0x000000) ; see http://msdn.microsoft.com/ms724940.aspx
DllCall("SystemParametersInfo", UInt, 0x14, UInt, 0, Str, "", UInt, 0x03) ; see http://msdn.microsoft.com/ms724947.aspx
Sleep, %Delay%
Send, {Printscreen}
Sleep, %Delay%
RunWait, %ImageMagickPath%\convert clipboard:myimage black_screen_capture.png, , Hide
; white background capture
DllCall("SetSysColors", Int, 1, IntP, 1, UIntP, 0xFFFFFF)
Sleep, %Delay%
Send, {Printscreen}
Sleep, %Delay%
RunWait, %ImageMagickPath%\convert clipboard:myimage white_screen_capture.png, , Hide
; restore original wallpaper and background color
DllCall("SetSysColors", Int, 1, IntP, 1, UIntP, BackgroundColorBackup)
FileCopy, %WallpaperBackupPath%, %WallpaperPath%
DllCall("SystemParametersInfo", UInt, 0x14, UInt, 0, Str, WallpaperPath, UInt, 0x03)
; show desktop taskbar and Start button
WinShow ahk_class Shell_TrayWnd
WinShow %StartButtonName% ahk_class Button
; show desktop icons if previously hidden
If (HideIcons = 0) {
ControlGet, HWND, Hwnd,, SysListView321, ahk_class Progman
If HWND =
ControlGet, HWND, Hwnd,, SysListView321, ahk_class WorkerW
WinShow, ahk_id %HWND%
}
; combine black and white captures
RunWait, %ImageMagickPath%\convert black_screen_capture.png white_screen_capture.png -alpha off ( -clone 0`,1 -compose difference -composite -negate ) ( -clone 0`,2 +swap -compose divide -composite ) -delete 0`,1 +swap -compose Copy_Opacity -composite -trim transparent_screen_capture_%A_Now%.png, , Hide
FileDelete, black_screen_capture.png
FileDelete, white_screen_capture.png
return
CleanUp:
FileDelete, %WallpaperBackupPath%
ExitApp
Examples
I'd love to get feedback from other users, especially if they have different configurations than mine. I'd also appreciate any suggestion to improve the code.