Jump to content

_WinAPI_CreateCompatibleBitmap to file


Recommended Posts

Hi guys

I'm seaching an alternative way to ScreenCapture UDF. Why? I think GDI+ is slow if you need to capture many images  :D

Searching on the web for the fastest method i have found:

1) Using Surface DirectX method

2) Using BitBlt method

The Directx method ( here you can find and example in C# ) is probably tha fastest but it's totally out of my knowledge, and i'm not sure can be done in autoit.

For the second i'm sure can be done in autoit and this is what i have make:

#include <WinAPI.au3>
;~ #include <ScreenCapture.au3> ; Testin purpose

Global Const $SRCCOPY = 0x00CC0020

$hDDC = _WinAPI_GetDC(0)
$hCDC = _WinAPI_CreateCompatibleDC($hDDC)
$hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, @DesktopWidth, @DesktopHeight)
_WinAPI_SelectObject($hCDC, $hBMP)
_WinAPI_BitBlt($hCDC, 0, 0, @DesktopWidth, @DesktopHeight, $hDDC, 0, 0, $SRCCOPY)
_WinAPI_ReleaseDC(0, $hDDC)
_WinAPI_DeleteDC($hCDC)
;~ _ScreenCapture_SaveImage(@ScriptDir & "\GDIPlus_Image.jpg", $hBMP) ; testing purpose
_WinAPI_SaveBMP(@ScriptDir & "\GDIPlus_Image.bmp", $hBMP) ; BMP is sure, PNG if possible. 

Func _WinAPI_SaveBMP($sFilename, $hData)
    $file = _WinAPI_CreateFile($sFileName)
    _WinAPI_CloseHandle($file)
;~  _WinAPI_WriteFile ; ---> Write file header and info header
;~  _WinAPI_CloseHandle($file) ; Close the handle
;~  _WinAPI_WriteFile ; ---> Write data of the bitmap
;~  _WinAPI_CloseHandle($file)
EndFunc

As you can see, work if i use GDI-ScreenCapture UDF but the goal is avoid that, if i need to use GDI i'll use the UDF :D

The main problem is i don't know how to write in the file the information stored in _WinAPI_CreateCompatibleBitmap ( so Save HBITMAP to file trans.gif?cver=0%0D%0A)

I need to create the file header, info header and then write the data? ( i think :D ) but i'm here because i need an help for do the last part of the code.

Thanks

Edited by johnmcloud
Link to comment
Share on other sites

Try this:

 

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

Global Const $SRCCOPY = 0x00CC0020
_GDIPlus_Startup()

Local $t = TimerInit()
Local $hWnd = _WinAPI_GetDesktopWindow()
Local $hDDC = _WinAPI_GetDC($hWnd)
Local $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
Local $hHBMP = _WinAPI_CreateCompatibleBitmap($hDDC, @DesktopWidth, @DesktopHeight)
_WinAPI_SelectObject($hCDC, $hHBMP)
_WinAPI_BitBlt($hCDC, 0, 0, @DesktopWidth, @DesktopHeight, $hDDC, 0, 0, $SRCCOPY)
Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBMP)
_GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\Test.jpg")
_GDIPlus_BitmapDispose($hBitmap)
_WinAPI_DeleteObject($hHBMP)
_WinAPI_ReleaseDC($hWnd, $hDDC)
_WinAPI_DeleteDC($hCDC)
ConsoleWrite(TimerDiff($t) & @LF)

_GDIPlus_Shutdown()
Exit
 

Br,

UEZ

Edited by UEZ

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

Hi UEZ, thanks for the answer.

As i said before, i'd like to avoid gdi. I'd like to make at least 1 screen every 41ms, with the _ScreenCapture UDF( it use gdi ) i can make 1 screen every 100ms

I'll test your solution and let you know, anyway i'm searching a method like c++ with saving file creating header etc.

Link to comment
Share on other sites

Look at WinAPIEx to get some ideas how to save GDI bitmaps. I don't know whether those methods are faster. Try it.

You can speed up the code above by using directly the calls instead of calling functions. This can give you some ms back.

Br,

UEZ

Edited by UEZ

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

Here the nacked version from post#2:

#include <GDIPlus.au3>

Global Const $SRCCOPY = 0x00CC0020
_GDIPlus_Startup()
Local $hDLL_user32 = DllOpen("user32.dll")
Local $hDLL_gdi32 = DllOpen("gdi32.dll")
Local $hDLL_ole32 = DllOpen("ole32.dll")

Local $t = TimerInit()
Local $aResult = DllCall($hDLL_user32, "hwnd", "GetDesktopWindow")
Local $hWnd = $aResult[0]
$aResult = DllCall($hDLL_user32, "handle", "GetDC", "hwnd", $hWnd)
Local $hDDC = $aResult[0]
$aResult = DllCall($hDLL_gdi32, "handle", "CreateCompatibleDC", "handle", $hDDC)
Local $hCDC = $aResult[0]
$aResult = DllCall($hDLL_gdi32, "handle", "CreateCompatibleBitmap", "handle", $hDDC, "int", @DesktopWidth, "int", @DesktopHeight)
Local $hHBMP = $aResult[0]
DllCall($hDLL_gdi32, "handle", "SelectObject", "handle", $hCDC, "handle", $hHBMP)
DllCall($hDLL_gdi32, "bool", "BitBlt", "handle", $hCDC, "int", 0, "int", 0, "int", @DesktopWidth, "int", @DesktopHeight, "handle", $hDDC, "int", 0, "int", 0, "dword", $SRCCOPY)
$aResult = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromHBITMAP", "handle", $hHBMP, "handle", 0, "ptr*", 0)
Local $hBitmap = $aResult[3]
Local $sCLSID = "{557CF401-1A04-11D3-9A73-0000F81EF32E}" ;JPG
Local $tGUID = DllStructCreate($tagGUID)
DllCall($hDLL_ole32, "long", "CLSIDFromString", "wstr", $sCLSID, "struct*", $tGUID)
DllCall($ghGDIPDll, "int", "GdipSaveImageToFile", "handle", $hBitmap, "wstr", @ScriptDir & "\Test.jpg", "struct*", $tGUID, "struct*", 0)
DllCall($ghGDIPDll, "int", "GdipDisposeImage", "handle", $hBitmap)
DllCall($hDLL_gdi32, "bool", "DeleteObject", "handle", $hHBMP)
DllCall($hDLL_user32, "int", "ReleaseDC", "hwnd", $hWnd, "handle", $hDDC)
DllCall($hDLL_gdi32, "bool", "DeleteDC", "handle", $hCDC)
ConsoleWrite(TimerDiff($t) & @LF)

DllClose($hDLL_gdi32)
DllClose($hDLL_ole32)
DllClose($hDLL_user32)

_GDIPlus_Shutdown()
Exit

Br,

UEZ

Edited by UEZ

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

So UEZ, i have tested both version but give me the same result. i can make 1 screen every 45,5ms, much better then the ScreenCapture UDF ( but for this i don't have any doubt ) but not like the result i'd like to make, but we are very close ;)

This is the test script i have make for check the screen for second:

DirCreate(@ScriptDir & "\Test")

$iCounter = 1
$iFramePerSec = 24
$iPause = 1000 / $iFramePerSec
$time = TimerInit()

Do
    $iStart = TimerInit()
    ;code @ScriptDir & "\Test\" & $iCounter & ".jpg"
    $iPauseLess = TimerDiff($iStart)
    $iStart2 = TimerInit()
    Sleep(Int($iPause - $iPauseLess))
    $iCounter += 1
Until TimerDiff($time) >= 1000

My last try is to use the WinAPIEx for save the file instead of GDI, but i don't know how to start, there are many func there :D

Suggestion?

Thanks

Edited by johnmcloud
Link to comment
Share on other sites

Well, i have found in WinAPIEx the _WinAPI_SaveHBITMAPToFile. I think that funcion do many "unecessary check" before creating the file ( in comparison with a C++ version like this ) and is more slow than the GDI, only 71ms for second

So for now the best was my first version + saving image in gdi by uez, but the goal I have set for myself i was not able to reach. I'm open to any other ideas ;)

Edited by johnmcloud
Link to comment
Share on other sites

For me the real bottleneck seems to be the actual "save to disk" call.

#include <GDIPlus.au3>

Global Const $SRCCOPY = 0x00CC0020
_GDIPlus_Startup()
Local $hDLL_user32 = DllOpen("user32.dll")
Local $hDLL_gdi32 = DllOpen("gdi32.dll")
Local $hDLL_ole32 = DllOpen("ole32.dll")

Local $t = TimerInit()
Local $aResult = DllCall($hDLL_user32, "hwnd", "GetDesktopWindow")
Local $hWnd = $aResult[0]
$aResult = DllCall($hDLL_user32, "handle", "GetDC", "hwnd", $hWnd)
Local $hDDC = $aResult[0]
$aResult = DllCall($hDLL_gdi32, "handle", "CreateCompatibleDC", "handle", $hDDC)
Local $hCDC = $aResult[0]
$aResult = DllCall($hDLL_gdi32, "handle", "CreateCompatibleBitmap", "handle", $hDDC, "int", @DesktopWidth, "int", @DesktopHeight)
Local $hHBMP = $aResult[0]
DllCall($hDLL_gdi32, "handle", "SelectObject", "handle", $hCDC, "handle", $hHBMP)
DllCall($hDLL_gdi32, "bool", "BitBlt", "handle", $hCDC, "int", 0, "int", 0, "int", @DesktopWidth, "int", @DesktopHeight, "handle", $hDDC, "int", 0, "int", 0, "dword", $SRCCOPY)
$aResult = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromHBITMAP", "handle", $hHBMP, "handle", 0, "ptr*", 0)
Local $hBitmap = $aResult[3]
Local $sCLSID = "{557CF401-1A04-11D3-9A73-0000F81EF32E}" ;JPG
Local $tGUID = DllStructCreate($tagGUID)
DllCall($hDLL_ole32, "long", "CLSIDFromString", "wstr", $sCLSID, "struct*", $tGUID)
ConsoleWrite(TimerDiff($t) & @LF)
DllCall($ghGDIPDll, "int", "GdipSaveImageToFile", "handle", $hBitmap, "wstr", @ScriptDir & "\Test.jpg", "struct*", $tGUID, "struct*", 0)
ConsoleWrite(TimerDiff($t) & @LF)
DllCall($ghGDIPDll, "int", "GdipDisposeImage", "handle", $hBitmap)
DllCall($hDLL_gdi32, "bool", "DeleteObject", "handle", $hHBMP)
DllCall($hDLL_user32, "int", "ReleaseDC", "hwnd", $hWnd, "handle", $hDDC)
DllCall($hDLL_gdi32, "bool", "DeleteDC", "handle", $hCDC)
ConsoleWrite(TimerDiff($t) & @LF)

DllClose($hDLL_gdi32)
DllClose($hDLL_ole32)
DllClose($hDLL_user32)

_GDIPlus_Shutdown()
Exit

 

Two things to overcome this come to my mind.

Either use a second child process for the saving (use some kind of stack for the bitmaps).

Or maybe take a look in the example section for how to save bitmaps as blobs to an (in memory) sqlite database for later processing.

Link to comment
Share on other sites

I'm not totally conviced about the "save to disk" problem, i think is only a problem of "speed" of execution. If was a save to disk problem how can DirectX method saves 15-25ms at images? Tested a demo code on a couple of VM

Anyway i don't need a funcion faster like that but something in 41ms, so i can use a thing like _GDIPlus_SaveImageToStream for multiple images and than saves later?

Edited by johnmcloud
Link to comment
Share on other sites

What also came to my mind... saving pictures in such a short succession, wouldn't it be better to save them as a movie file? I remember seeing some code for that... by UEZ too?

Edit: Yep, by UEZ :), >here it is. I did not compare the performance, but might be worth a look. An option would be to save as avi and then, if really needed, to extract the single pictures afterwards

Edited by KaFu
Link to comment
Share on other sites

Many things comes to your mind :D

It save the hbitmap directly to AVI but the question is, how fast it is? It uses _ScreenCapture, from my test it's the worst method in terms of speed but now i'll check it and let you know

Edited by johnmcloud
Link to comment
Share on other sites

TESTED: If i put 24fps and one of the compression...the video is superfaster :D

If i use non-compressed format seems "normal" ( i don't know how many screen for second ) but the output for 5 second of video is 150MB.

Just for know, I don't have understand how to select the video compression without popop, example the Microsoft Video 1 with 100% Quality. I think is in the UDF, i don't have found any parameter in _CreateAVI

Edited by johnmcloud
Link to comment
Share on other sites

TESTED: If i put 24fps and one of the compression...the video is superfaster :D

If i use non-compressed format seems "normal" ( i don't know how many screen for second ) but the output for 5 second of video is 150MB.

Just for know, I don't have understand how to select the video compression without popop, example the Microsoft Video 1 with 100% Quality. I think is in the UDF, i don't have found any parameter in _CreateAVI

 

I guess that the answer to this questions might be buried somewhere in this >post (esp. the last post might give a clue).

Edited by KaFu
Link to comment
Share on other sites

I'm checking what i can do with AVIWriter UDF. I have only a problem, based on the example by UEZ posted by KaFu's post, if i set the $rec_duration = 10 ; ten second and i want to stop it with a hotkey, how i can do?

I have try to:

1) Exit the main loop

2) _CloseAvi

But everytime autoit crash with -1073741819. Thanks for any help

EDIT: Wait, is with the HotKeySet the problem. If i use:

Func _Test1()
 Start()
EndFunc

Func _Test2()
 Stop()
EndFunc

With two different hotkey i don't have problem, if i use the same hotkey in this way:

Func _StartStop()
    If $fFlag Then
        Start()
    Else
        Stop()
    EndIf
    $fFlag = Not $fFlag
EndFunc   ;==>_StartStop

Crash! Yes, i have declared the flag, if i use ConsoleWrite work...that's strange

Edited by johnmcloud
Link to comment
Share on other sites

Try something like this here:

#include "AVIWriter Extented.au3"
#include <Misc.au3>
#include <ScreenCapture.au3>
#include <WindowsConstants.au3>

_GDIPlus_Startup()

Global Const $sFile = @ScriptDir & "\test.avi"
FileDelete($sFile)

Global $rec_duration = 20 ;20 seconds
Global $fps = 10

Global Const $dll = DllOpen("user32.dll")
Global $hGUI_Grab2AVI = GUICreate("", 0, 0, 0, 0, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW, WinGetHandle(AutoItWinGetTitle()))
GUISetBkColor(0xFF0000, $hGUI_Grab2AVI)
GUISetState(@SW_SHOW, $hGUI_Grab2AVI)
Global $aMPos, $hWin, $hWinAncestor, $hWnd, $aRet_prev, $aPos
Global Const $frame_size = 3
Global $tPoint = DllStructCreate($tagPOINT)
Global $esc = True
Global $mxo, $myo

While Not _IsPressed("1B", $dll) * Sleep(30)
    $aMPos = MouseGetPos()
    DllStructSetData($tPoint, 1, $aMPos[0])
    DllStructSetData($tPoint, 2, $aMPos[1])
    $hWin = _WinAPI_WindowFromPoint($tPoint)
    $hWinAncestor = _WinAPI_GetAncestor($hWin, 2)
    $hWnd = HWnd($hWinAncestor)
    $aRet_prev = -1
    $aPos  = WinGetPos($hWnd)
    If $hWnd <> $hGUI_Grab2AVI And $hWnd <> $aRet_prev Then
        $aRet_prev = $hWnd
        If $aMPos[0] <> $mxo Or $aMPos[1] <> $myo Then
            WinMove($hGUI_Grab2AVI, "", $aPos[0], $aPos[1], $aPos[2], $aPos[3], 1)
            _GuiHole($hGUI_Grab2AVI, $frame_size, $frame_size, $aPos[2] - 2 * $frame_size, $aPos[3] - 2 * $frame_size, $aPos[2], $aPos[3])
            WinSetOnTop($hGUI_Grab2AVI, 0, 1)
            ToolTip("Press CTRL to start capturing of" & @LF & "marked window to a AVI file!" & @LF & @LF & _
                         "Duration: " & $rec_duration & " seconds @ " & $fps & " fps" & @LF & _
                          "Windows Size: " & $aPos[2] & " x " & $aPos[3], $aMPos[0] + 20, $aMPos[1] + 20)
            $mxo = $aMPos[0]
            $myo = $aMPos[1]
        EndIf
    EndIf
    If _IsPressed("11", $dll) Then
        $esc = False
        ExitLoop
    EndIf
WEnd
ToolTip("")
GUIDelete($hGUI_Grab2AVI)
If $esc Then Exit MsgBox(0, "Information", "Aborted", 10)

_StartAviLibrary()
Global $aAVI = _CreateAvi($sFile, $fps, $aPos[2], $aPos[3])
If @error Then close()

Global $hBmp

Global $fSleep = 1000 / $fps, $t, $td
Global $total_FPS = $rec_duration * $fps, $fps_c = 1
$k32_dll = DllOpen("kernel32.dll")
Do
    $fTimer = TimerInit()

    $hBmp = _ScreenCapture_CaptureWnd("", $hWnd)
    _AddHBitmapToAvi($aAVI, $hBmp)
    _WinAPI_DeleteObject($hBmp)

    $fps_c += 1
    $td = $fSleep - TimerDiff($fTimer)
    If $td > 0 Then
        DllCall($k32_dll, "none", "Sleep", "dword", $td)
    EndIf

    If $fps_c > $total_FPS Or _IsPressed("1B", $dll) Then
        Close()
    EndIf

Until False

Func Close()
    _GDIPlus_Shutdown()
    _CloseAvi($aAVI)
    _StopAviLibrary()
    ConsoleWrite($fps_c & " frames added" & @CRLF)
    ConsoleWrite("AVI filesize: " & Round(FileGetSize($sFile) / 1024 ^ 2, 2) & " MB" & @CRLF)
    DllClose($k32_dll)
    DllClose($dll)
    Exit MsgBox(0, "Information", "Done!", 15)
EndFunc   ;==>close

Func _GuiHole($hWnd, $i_x, $i_y, $i_sizew, $i_sizeh, $width, $height)
    Local $outer_rgn, $inner_rgn, $combined_rgn
    $outer_rgn = _WinAPI_CreateRectRgn(0, 0, $width, $height)
    $inner_rgn = _WinAPI_CreateRectRgn($i_x, $i_y, $i_x + $i_sizew, $i_y + $i_sizeh)
    $combined_rgn = _WinAPI_CreateRectRgn(0, 0, 0, 0)
    _WinAPI_CombineRgn($combined_rgn, $outer_rgn, $inner_rgn, $RGN_DIFF)
    _WinAPI_DeleteObject($outer_rgn)
    _WinAPI_DeleteObject($inner_rgn)
    _WinAPI_SetWindowRgn($hWnd, $combined_rgn)
EndFunc   ;==>_GuiHole

Br,

UEZ

Edited by UEZ

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

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

×
×
  • Create New...