Jump to content

Recommended Posts

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


$hwnd = WinGetHandle("Program Manager")
$hBitmap = Capture_Window($hwnd, 1920, 1080)

$hClone  = _GDIPlus_BitmapCloneArea($hBitmap, 818, 521, 81, 80,  $GDIP_PXF16GRAYSCALE)
_GDIPlus_ImageSaveToFile($hClone, "C:\Users\CAIO\Downloads\TESTE.bmp")


Func Capture_Window($hWnd, $w, $h)
    If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0)
    If Int($w) < 1 Then Return SetError(2, 0, 0)
    If Int($h) < 1 Then Return SetError(3, 0, 0)
    Local Const $hDC_Capture = _WinAPI_GetDC(HWnd($hWnd))
    Local Const $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture)
    Local Const $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $w, $h)
    Local Const $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap)
    DllCall("gdi32.dll", "int", "SetStretchBltMode", "hwnd", $hDC_Capture, "uint", 4)
    DllCall("user32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0)

    _WinAPI_DeleteDC($hMemDC)
    _WinAPI_SelectObject($hMemDC, $hObjectOld)
    _WinAPI_ReleaseDC($hWnd, $hDC_Capture)

    Local Const $hFullScreen = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]")
    Local Const $aFullScreen = WinGetPos($hFullScreen)
    Local Const $c1 = $aFullScreen[2] - @DesktopWidth, $c2 = $aFullScreen[3] - @DesktopHeight
    Local Const $wc1 = $w - $c1, $hc2 = $h - $c2

    If (($wc1 > 1 And $wc1 < $w) Or ($w - @DesktopWidth > 1) Or ($hc2 > 7 And $hc2 < $h) Or ($h - @DesktopHeight > 1)) And (BitAND(WinGetState(HWnd($hWnd)), 32) = 32) Then
        Local $hBmp_t = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
        $hBmp = _GDIPlus_BitmapCloneArea($hBmp_t, 8, 8, $w - 16, $h - 16)
        _GDIPlus_BitmapDispose($hBmp_t)
    Else
        $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
    EndIf
    _WinAPI_DeleteObject($hHBitmap)
    Return $hBmp
EndFunc   ;==>Capture_Window

I have this script to capture image from background window, then crop a area, and save with a format:

https://www.autoitscript.com/autoit3/docs/libfunctions/_GDIPlus_BitmapCloneArea.htm

The problem is: i need save it as grayscale, but i get no image when i try saving it as $GDIP_PXF16GRAYSCALE, the other formats works.

Google $GDIP_PXF16GRAYSCALE and found others post from people complaining about it, so its a "bug" or i'm doing something wrong?

I could achieve my cropped img saved as gray scale by other manner? 

Edited by memerim
Link to post
Share on other sites
#include <GDIPlus.au3>
    #include <ScreenCapture.au3>


    $hwnd = WinGetHandle("Program Manager")
    ;$hBitmap = Capture_Window($hwnd, 1920, 1080)
    ;$hClone     = _GDIPlus_BitmapCloneArea($hBitmap, 818, 521, 81, 80,  $GDIP_PXF08INDEXED)
    ;_GDIPlus_ImageSaveToFile($hClone, "C:\Users\CAIO\Downloads\TESTE.bmp")



    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hwnd ) ;create a graphics object from a window handle
    Local $hIA = _GDIPlus_ImageAttributesCreate() ;create an ImageAttribute object

    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateGrayScale() ;create greyscale color matrix

    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix) ;set greyscale color matrix

    Local $hHBmp = Capture_Window($hwnd, 1920, 1080)
    $hClone  = _GDIPlus_BitmapCloneArea($hHBmp, 818, 521, 81, 80)

    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hClone) ;convert GDI to GDI+ bitmap
    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore


    $hBmp2 = _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, 81, 80, 0, 0, 81, 80, $hIA) ;draw the bitmap while applying the color adjustment

    _GDIPlus_ImageSaveToFile($hBmp2, "C:\Users\CAIO\Downloads\TESTE.bmp")



Func Capture_Window($hWnd, $w, $h)
    If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0)
    If Int($w) < 1 Then Return SetError(2, 0, 0)
    If Int($h) < 1 Then Return SetError(3, 0, 0)
    Local Const $hDC_Capture = _WinAPI_GetDC(HWnd($hWnd))
    Local Const $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture)
    Local Const $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $w, $h)
    Local Const $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap)
    DllCall("gdi32.dll", "int", "SetStretchBltMode", "hwnd", $hDC_Capture, "uint", 4)
    DllCall("user32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0)

    _WinAPI_DeleteDC($hMemDC)
    _WinAPI_SelectObject($hMemDC, $hObjectOld)
    _WinAPI_ReleaseDC($hWnd, $hDC_Capture)

    Local Const $hFullScreen = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]")
    Local Const $aFullScreen = WinGetPos($hFullScreen)
    Local Const $c1 = $aFullScreen[2] - @DesktopWidth, $c2 = $aFullScreen[3] - @DesktopHeight
    Local Const $wc1 = $w - $c1, $hc2 = $h - $c2

    If (($wc1 > 1 And $wc1 < $w) Or ($w - @DesktopWidth > 1) Or ($hc2 > 7 And $hc2 < $h) Or ($h - @DesktopHeight > 1)) And (BitAND(WinGetState(HWnd($hWnd)), 32) = 32) Then
        Local $hBmp_t = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
        $hBmp = _GDIPlus_BitmapCloneArea($hBmp_t, 8, 8, $w - 16, $h - 16)
        _GDIPlus_BitmapDispose($hBmp_t)
    Else
        $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
    EndIf
    _WinAPI_DeleteObject($hHBitmap)
    Return $hBmp
EndFunc   ;==>Capture_Window

Its too many functions mixed up, i've tried this way above, but i still dont got the image saved🤔

And about the $GDIP_PXF16GRAYSCALE, its not working?

Link to post
Share on other sites

@memerim  There is multiple problems in your script, but the most important is you did not insert any error checks.  You only verify the end result and of course the .bmp was not created.  So to solve your issues, do the following :

1- Insert after every statement a ConsoleWrite that shows the result of the previous return AND the content of @error

2- to help you out, the graphic creation is Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hClone) not the way you created it

3- dispose all the elements  $hIA, $hGraphic, etc...

4- the return of Capture_Window is bitmap not a Hbitmap

5- You are missing the very first and very last statements when using GDI+

6- Reading help file and understanding what you programmed would also greatly help you

Link to post
Share on other sites

Try something like this here:

Spoiler
#include <ScreenCapture.au3>


$hwnd = WinGetHandle("Program Manager")
;$hBitmap = Capture_Window($hwnd, 1920, 1080)
;$hClone     = _GDIPlus_BitmapCloneArea($hBitmap, 818, 521, 81, 80,  $GDIP_PXF08INDEXED)
;_GDIPlus_ImageSaveToFile($hClone, "C:\Users\CAIO\Downloads\TESTE.bmp")
_GDIPlus_Startup()
Local $hIA = _GDIPlus_ImageAttributesCreate() ;create an ImageAttribute object
Local $tColorMatrix = _GDIPlus_ColorMatrixCreateGrayScale() ;create greyscale color matrix
_GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix) ;set greyscale color matrix

Local $iWidth = 81, $iHeight = 80
Local $hHBmp = Capture_Window($hwnd, @DesktopWidth, @DesktopHeight)
Local $hClone  = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
Local $hGfx = _GDIPlus_ImageGetGraphicsContext($hClone)
_GDIPlus_GraphicsDrawImageRectRect($hGfx, $hHBmp, 0, 0, $iWidth, $iHeight, 0, 0, $iWidth, $iHeight, $hIA) ;draw the bitmap while applying the color adjustment
_GDIPlus_ImageSaveToFile($hClone, "TESTE.bmp")

_GDIPlus_ImageAttributesDispose($hIA)
_GDIPlus_ImageDispose($hHBmp)
_GDIPlus_ImageDispose($hClone)
_GDIPlus_GraphicsDispose($hGfx)
_GDIPlus_Shutdown()


Func Capture_Window($hWnd, $w, $h)
    If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0)
    If Int($w) < 1 Then Return SetError(2, 0, 0)
    If Int($h) < 1 Then Return SetError(3, 0, 0)
    Local Const $hDC_Capture = _WinAPI_GetDC(HWnd($hWnd))
    Local Const $hMemDC = _WinAPI_CreateCompatibleDC($hDC_Capture)
    Local Const $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Capture, $w, $h)
    Local Const $hObjectOld = _WinAPI_SelectObject($hMemDC, $hHBitmap)
    DllCall("gdi32.dll", "int", "SetStretchBltMode", "hwnd", $hDC_Capture, "uint", 4)
    DllCall("user32.dll", "int", "PrintWindow", "hwnd", $hWnd, "handle", $hMemDC, "int", 0)

    _WinAPI_DeleteDC($hMemDC)
    _WinAPI_SelectObject($hMemDC, $hObjectOld)
    _WinAPI_ReleaseDC($hWnd, $hDC_Capture)

    Local Const $hFullScreen = WinGetHandle("[TITLE:Program Manager;CLASS:Progman]")
    Local Const $aFullScreen = WinGetPos($hFullScreen)
    Local Const $c1 = $aFullScreen[2] - @DesktopWidth, $c2 = $aFullScreen[3] - @DesktopHeight
    Local Const $wc1 = $w - $c1, $hc2 = $h - $c2

    If (($wc1 > 1 And $wc1 < $w) Or ($w - @DesktopWidth > 1) Or ($hc2 > 7 And $hc2 < $h) Or ($h - @DesktopHeight > 1)) And (BitAND(WinGetState(HWnd($hWnd)), 32) = 32) Then
        Local $hBmp_t = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
        $hBmp = _GDIPlus_BitmapCloneArea($hBmp_t, 8, 8, $w - 16, $h - 16)
        _GDIPlus_BitmapDispose($hBmp_t)
    Else
        $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($hHBitmap)
    EndIf
    _WinAPI_DeleteObject($hHBitmap)
    Return $hBmp
EndFunc   ;==>Capture_Window

 

 

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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...