Jump to content
Sign in to follow this  
johnmcloud

_WinAPI_CreateCompatibleBitmap to file

Recommended Posts

johnmcloud

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

Share this post


Link to post
Share on other sites
UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
johnmcloud

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.

Share this post


Link to post
Share on other sites
UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
johnmcloud

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

Share this post


Link to post
Share on other sites
johnmcloud

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

Share this post


Link to post
Share on other sites
KaFu

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.

Share this post


Link to post
Share on other sites
johnmcloud

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

Share this post


Link to post
Share on other sites
KaFu

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

Share this post


Link to post
Share on other sites
johnmcloud

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

Share this post


Link to post
Share on other sites
johnmcloud

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

Share this post


Link to post
Share on other sites
KaFu

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

Share this post


Link to post
Share on other sites
johnmcloud

Damn, you have right. The code was commented out in UDF I have think was unecessary/unused code

Share this post


Link to post
Share on other sites
johnmcloud

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

Share this post


Link to post
Share on other sites
UEZ

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


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
Sign in to follow this  

  • Similar Content

    • VADemon
      By VADemon
      I've encountered a problem with a single file where I cannot retrieve it's Date-time. So far my code has worked well for over 30 files, but this one is a mystery I cannot debug myself due to insufficient Au3 knowledge.
      In line 11 "_Date_Time_FileTimeToArray" is called and for this particular file it sets the @error to 10. I don't know what that error code means, but it's not set by the _Date functions themselves I think.
      Overall, it could be a problem caused by any of the functions below, how can I properly debug this? / Does anybody know a what's causing this?
      _WinAPI_CreateFile() / _Date_Time_GetFileTime() / _Date_Time_FileTimeToArray()
      Func _SetFileTimes($sFilePath) Local $monthNumber[13] = ["", "January", "February", "March", "April", "May", "Juny", "July", "August", "September", "October", "November", "December"] Local $dayNumber[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only ; may NOT return a valid date for some reason! TODO Local $fTagFILETIME = _Date_Time_GetFileTime($fHandle) _WinAPI_CloseHandle($fHandle) ; This will return an empty array if theres no valid date $fModTime = _Date_Time_FileTimeToArray($fTagFILETIME[2]) ; last Modified if @error <> 10 then Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] Global $prettyTimestamp = StringFormat("%s, %s %d, %04d %02d:%02d:%02d", $dayNumber[$weekday], $monthNumber[$month], $day, $year, $hour, $min, $sec) Global $uploadDate = StringFormat("%04d-%02d-%02d", $year, $month, $day) $fModTime = _Date_Time_FileTimeToArray(_Date_Time_FileTimeToLocalFileTime($fTagFILETIME[2])) ; last Modified Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] ; GetUnixTime accounts for Local time, hence feed it local time Global $unixTimestamp = _GetUnixTime($year &"/"& $month &"/"& $day &" "& $hour&":"& $min &":"& $sec) else Global $prettyTimestamp = "N/A" Global $uploadDate = "" Global $unixTimestamp = "N/A" endif endfunc  
      _GetUnixTime returned the year 1601 start date, showing that $fModTime is probably equal 0. (But Why?)
      The file reports these dates in Explorer, it's on local NTFS drive:
      Created: ‎‎Wednesday, ‎31. ‎Januar ‎2018, ‏‎18:55:02
      Modified: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
      Accessed: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
    • astrionn
      By astrionn
      Hi Guys,
       
      I`m trying to record with my webcam in 5s intervalls and do some stuff between starting to record and stoping.
      I took code from here: https://www.autoitscript.com/forum/topic/27925-webcam-example/
      and took what looked relevant to me.
      I use 3 different files:
       Is called WMS.au3 and contains all the constants for the dll calls is called rec.au3 and does all necessary dll calls to start a webcam recording to a file Is called stopcam.exe and does the dll calls to stop recording into a file WMS.au3  :
      $WM_CAP_START = 0x400 $WM_CAP_UNICODE_START = $WM_CAP_START +100 $WM_CAP_PAL_SAVEA = $WM_CAP_START + 81 $WM_CAP_PAL_SAVEW = $WM_CAP_UNICODE_START + 81 $WM_CAP_UNICODE_END = $WM_CAP_PAL_SAVEW $WM_CAP_ABORT = $WM_CAP_START + 69 $WM_CAP_DLG_VIDEOCOMPRESSION = $WM_CAP_START + 46 $WM_CAP_DLG_VIDEODISPLAY = $WM_CAP_START + 43 $WM_CAP_DLG_VIDEOFORMAT = $WM_CAP_START + 41 $WM_CAP_DLG_VIDEOSOURCE = $WM_CAP_START + 42 $WM_CAP_DRIVER_CONNECT = $WM_CAP_START + 10 $WM_CAP_DRIVER_DISCONNECT = $WM_CAP_START + 11 $WM_CAP_DRIVER_GET_CAPS = $WM_CAP_START + 14 $WM_CAP_DRIVER_GET_NAMEA = $WM_CAP_START + 12 $WM_CAP_DRIVER_GET_NAMEW = $WM_CAP_UNICODE_START + 12 $WM_CAP_DRIVER_GET_VERSIONA = $WM_CAP_START + 13 $WM_CAP_DRIVER_GET_VERSIONW = $WM_CAP_UNICODE_START + 13 $WM_CAP_EDIT_COPY = $WM_CAP_START + 30 $WM_CAP_END = $WM_CAP_UNICODE_END $WM_CAP_FILE_ALLOCATE = $WM_CAP_START + 22 $WM_CAP_FILE_GET_CAPTURE_FILEA = $WM_CAP_START + 21 $WM_CAP_FILE_GET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 21 $WM_CAP_FILE_SAVEASA = $WM_CAP_START + 23 $WM_CAP_FILE_SAVEASW = $WM_CAP_UNICODE_START + 23 $WM_CAP_FILE_SAVEDIBA = $WM_CAP_START + 25 $WM_CAP_FILE_SAVEDIBW = $WM_CAP_UNICODE_START + 25 $WM_CAP_FILE_SET_CAPTURE_FILEA = $WM_CAP_START + 20 $WM_CAP_FILE_SET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 20 $WM_CAP_FILE_SET_INFOCHUNK = $WM_CAP_START + 24 $WM_CAP_GET_AUDIOFORMAT = $WM_CAP_START + 36 $WM_CAP_GET_CAPSTREAMPTR = $WM_CAP_START + 1 $WM_CAP_GET_MCI_DEVICEA = $WM_CAP_START + 67 $WM_CAP_GET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 67 $WM_CAP_GET_SEQUENCE_SETUP = $WM_CAP_START + 65 $WM_CAP_GET_STATUS = $WM_CAP_START + 54 $WM_CAP_GET_USER_DATA = $WM_CAP_START + 8 $WM_CAP_GET_VIDEOFORMAT = $WM_CAP_START + 44 $WM_CAP_GRAB_FRAME = $WM_CAP_START + 60 $WM_CAP_GRAB_FRAME_NOSTOP = $WM_CAP_START + 61 $WM_CAP_PAL_AUTOCREATE = $WM_CAP_START + 83 $WM_CAP_PAL_MANUALCREATE = $WM_CAP_START + 84 $WM_CAP_PAL_OPENA = $WM_CAP_START + 80 $WM_CAP_PAL_OPENW = $WM_CAP_UNICODE_START + 80 $WM_CAP_PAL_PASTE = $WM_CAP_START + 82 $WM_CAP_SEQUENCE = $WM_CAP_START + 62 $WM_CAP_SEQUENCE_NOFILE = $WM_CAP_START + 63 $WM_CAP_SET_AUDIOFORMAT = $WM_CAP_START + 35 $WM_CAP_SET_CALLBACK_CAPCONTROL = $WM_CAP_START + 85 $WM_CAP_SET_CALLBACK_ERRORA = $WM_CAP_START + 2 $WM_CAP_SET_CALLBACK_ERRORW = $WM_CAP_UNICODE_START + 2 $WM_CAP_SET_CALLBACK_FRAME = $WM_CAP_START + 5 $WM_CAP_SET_CALLBACK_STATUSA = $WM_CAP_START + 3 $WM_CAP_SET_CALLBACK_STATUSW = $WM_CAP_UNICODE_START + 3 $WM_CAP_SET_CALLBACK_VIDEOSTREAM = $WM_CAP_START + 6 $WM_CAP_SET_CALLBACK_WAVESTREAM = $WM_CAP_START + 7 $WM_CAP_SET_CALLBACK_YIELD = $WM_CAP_START + 4 $WM_CAP_SET_MCI_DEVICEA = $WM_CAP_START + 66 $WM_CAP_SET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 66 $WM_CAP_SET_OVERLAY = $WM_CAP_START + 51 $WM_CAP_SET_PREVIEW = $WM_CAP_START + 50 $WM_CAP_SET_PREVIEWRATE = $WM_CAP_START + 52 $WM_CAP_SET_SCALE = $WM_CAP_START + 53 $WM_CAP_SET_SCROLL = $WM_CAP_START + 55 $WM_CAP_SET_SEQUENCE_SETUP = $WM_CAP_START + 64 $WM_CAP_SET_USER_DATA = $WM_CAP_START + 9 $WM_CAP_SET_VIDEOFORMAT = $WM_CAP_START + 45 $WM_CAP_SINGLE_FRAME = $WM_CAP_START + 72 $WM_CAP_SINGLE_FRAME_CLOSE = $WM_CAP_START + 71 $WM_CAP_SINGLE_FRAME_OPEN = $WM_CAP_START + 70 $WM_CAP_STOP = $WM_CAP_START + 68 rec.au3  :
       
      #include <WMS.au3> #include <GUIConstants.au3> if not FileExists(@ScriptDir&"\vid\"&string(@MON)&"\"&string(@mday)) Then DirCreate(@ScriptDir&"\vid\"&"\"&string(@MON)&"\"&string(@mday)) EndIf $moviefile = @ScriptDir&"\vid"&"\"&string(@MON)&"\"&string(@mday)&"\test.avi" $avi = DllOpen("avicap32.dll") $user = DllOpen("user32.dll") FileWrite(@scriptdir&"\clop.clop",$user) $Main = GUICreate("Camera",350,270) $moviefile=@ScriptDir&"\test.avi" $cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,$WS_VISIBLE), "int", 15, "int", 15, "int", 320, "int", 240, "hwnd", $Main, "int", 1) FileWrite(@scriptdir&"\clip.clip",$cap[0]) run(@scriptdir&"\stopcam.exe") DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_DRIVER_CONNECT, "int", 0, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_SCALE, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_OVERLAY, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEW, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEWRATE, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_FILE_SET_CAPTURE_FILEA, "int", 0, "str", $moviefile) DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SEQUENCE, "int", 0, "int", 0) Stopcam.exe is the compiled version of this:
      #include "WMS.au3" #include <GUIConstants.au3> $cap = Int(FileRead(@scriptdir&"\clip.clip")) $user = int(fileread(@scriptdir&"\clop.clop")) Sleep(10000) DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_STOP, "int", 0, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_END, "int", 0, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_DRIVER_DISCONNECT, "int", 0, "int", 0) DllClose($user) FileDelete(@scriptdir&"\clip.clip") FileDelete(@scriptdir&"\clop.clop")  
      Unfortunately this is not working as I thought it would be working.
      Does someone see what I´m doing wrong?
      It should record the webcam for about 10 seconds ( a little less) and save that to an .avi file.
      It does not stop the recording , I have to stop the process manually...
      Secondly, which confuses me more is that it creates the test.avi not in the location is specified in the script... its stored in the ScriptDir for whatever reason haha
      I hope you can help me
       
      ~HappyCoding
    • badcoder123
      By badcoder123
      I was looking around the help file and I came across _GDIPlus_GraphicsDrawRect and _WinAPI_DrawLine and I seem to be having the same "problem" where every time it draws into a window and you lose or gain focus it has to redraw itself. Is there anyway around this? 
      #include <WindowsConstants.au3> #include <WinAPI.au3> #Include <GDIPlus.au3> _GDIPlus_Startup() Run("notepad.exe") $hWnd = WinWait("Untitled") $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd) $Color = 0xFFFF0000 $hPen = _GDIPlus_PenCreate($Color, 2) For $i = 1 To 10 _GDIPlus_GraphicsDrawRect($hGraphic, 200, 200, 25 ,25, $hPen) ToolTip($i) Sleep(1000) Next _WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_PenDispose($hPen) _GDIPlus_Shutdown()  
    • ScriptJunky
      By ScriptJunky
      I noticed a lack of a constants file for _WinAPI_GetSystemMetrics() so I made this for anyone who wants to add it to their library. Enjoy!  (file attached below)
      #include-once ; #INDEX# ======================================================================================================================= ; Title .........: WinAPI GetSystemMetrics Constants ; AutoIt Version : 3.3.14.5 ; Language ......: English ; Description ...: Constants for _WinAPI_GetSystemMetrics(). ; Author(s) .....: ScriptJunky ; =============================================================================================================================== ; #CONSTANTS# =================================================================================================================== ; _WinAPI_GetSystemMetrics() Global Const $ARRANGE = 56 Global Const $CLEANBOOT = 67 Global Const $CMONITORS = 80 Global Const $CMOUSEBUTTONS = 43 Global Const $CONVERTIBLESLATEMODE = 0x2003 Global Const $CXBORDER = 5 Global Const $CXCURSOR = 13 Global Const $CXDLGFRAME = 7 Global Const $CXDOUBLECLK = 36 Global Const $CXDRAG = 68 Global Const $CXEDGE = 45 Global Const $CXFIXEDFRAME = 7 Global Const $CXFOCUSBORDER = 83 Global Const $CXFRAME = 32 Global Const $CXFULLSCREEN = 16 Global Const $CXHSCROLL = 21 Global Const $CXHTHUMB = 10 Global Const $CXICON = 11 Global Const $CXICONSPACING = 38 Global Const $CXMAXIMIZED = 61 Global Const $CXMAXTRACK = 59 Global Const $CXMENUCHECK = 71 Global Const $CXMENUSIZE = 54 Global Const $CXMIN = 28 Global Const $CXMINIMIZED = 57 Global Const $CXMINSPACING = 47 Global Const $CXMINTRACK = 34 Global Const $CXPADDEDBORDER = 92 Global Const $CXSCREEN = 0 Global Const $CXSIZE = 30 Global Const $CXSIZEFRAME = 32 Global Const $CXSMICON = 49 Global Const $CXSMSIZE = 52 Global Const $CXVIRTUALSCREEN = 78 Global Const $CXVSCROLL = 2 Global Const $CYBORDER = 6 Global Const $CYCAPTION = 4 Global Const $CYCURSOR = 14 Global Const $CYDLGFRAME = 8 Global Const $CYDOUBLECLK = 37 Global Const $CYDRAG = 69 Global Const $CYEDGE = 46 Global Const $CYFIXEDFRAME = 8 Global Const $CYFOCUSBORDER = 84 Global Const $CYFRAME = 33 Global Const $CYFULLSCREEN = 17 Global Const $CYHSCROLL = 3 Global Const $CYICON = 12 Global Const $CYICONSPACING = 39 Global Const $CYKANJIWINDOW = 18 Global Const $CYMAXIMIZED = 62 Global Const $CYMAXTRACK = 60 Global Const $CYMENU = 15 Global Const $CYMENUCHECK = 72 Global Const $CYMENUSIZE = 55 Global Const $CYMIN = 29 Global Const $CYMINIMIZED = 58 Global Const $CYMINSPACING = 48 Global Const $CYMINTRACK = 35 Global Const $CYSCREEN = 1 Global Const $CYSIZE = 31 Global Const $CYSIZEFRAME = 33 Global Const $CYSMCAPTION = 51 Global Const $CYSMICON = 50 Global Const $CYSMSIZE = 53 Global Const $CYVIRTUALSCREEN = 79 Global Const $CYVSCROLL = 20 Global Const $CYVTHUMB = 9 Global Const $DBCSENABLED = 42 Global Const $DEBUG = 22 Global Const $DIGITIZER = 94 Global Const $IMMENABLED = 82 Global Const $MAXIMUMTOUCHES = 95 Global Const $MEDIACENTER = 87 Global Const $MENUDROPALIGNMENT = 40 Global Const $MIDEASTENABLED = 74 Global Const $MOUSEPRESENT = 19 Global Const $MOUSEHORIZONTALWHEELPRESENT = 91 Global Const $MOUSEWHEELPRESENT = 75 Global Const $NETWORK = 63 Global Const $PENWINDOWS = 41 Global Const $REMOTECONTROL = 0x2001 Global Const $REMOTESESSION = 0x1000 Global Const $SAMEDISPLAYFORMAT = 81 Global Const $SECURE = 44 Global Const $SERVERR = 289 Global Const $SHOWSOUNDS = 70 Global Const $SHUTTINGDOWN = 0x2000 Global Const $SLOWMACHINE = 73 Global Const $STARTER = 88 Global Const $SWAPBUTTON = 23 Global Const $TABLETPC = 86 Global Const $XVIRTUALSCREEN = 76 Global Const $YVIRTUALSCREEN = 77  
      WinAPISystemMetricsConstants.au3
    • chacoya121
      By chacoya121
      can someone plz explain how WinAPI work and some example script plz
×