Jump to content

Recommended Posts

Posted (edited)

Hi,

I would to know if someone has already tried to capture snapshot from webcam without file writing (No $WM_CAP_FILE_SAVEDIBA)

I semi succes on that but some webcam retrieve 16bits pixel structure (instead of 26bit) and I don't know how to exploit that.

When the webcam send a 16bit pixel structure, i do my "CreateBmpByStructBITMAPINFOAndPixels" but biCompression is equals to 0x32595559 and if i write that in the BITMAPINFO struct, then the picture is corrumpted, so; I force it at 0 to have a "pseudo-picture" -> (we can see shapes but colors are bad ang image is verticaly inverted)

There is my research, if you use manycam, that should work but not if you use a real webcam

(If you have the probleme, please, help me to fix it ^^)

Exemple.au3

#include <gdiplus.au3>
#include <guiconstantsex.au3>
#include <winapi.au3>
#include "Bitmap_String.au3"
#include "PixelsToBmp.au3"
#include "Webcam.au3"

Opt("GUIOnEventMode", 1)

Global $GuiCam
Global $GuiInterception
Global $camera
Global $Frame

HotKeySet("²", "snap")


_GDIPlus_Startup()
$GuiCam = GUICreate("GUICamera", 320, 240)
GUISetState()

$GuiInterception = GUICreate("GUI interception frames", 320, 240)
$Frame = GUICtrlCreatePic("", 0, 0, 320, 240)
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "quit", $GuiCam)
GUISetOnEvent($GUI_EVENT_CLOSE, "quit", $GuiInterception)

$camera = _WebcamOpen($GuiCam,0,0,320,240)
_WebcamSetCallBackFram($camera, "frameCallBack")

While(True)
    Sleep(10)
WEnd

Func snap()
    _WebcamGetFrame($camera)
EndFunc

Func frameCallBack($Hwnd, $param1)
    Local $videohdr_tag = "PTR lpData;DWORD dwBufferLength;DWORD dwBytesUsed;DWORD dwTimeCaptured;DWORD_PTR dwUser;DWORD dwFlags;DWORD_PTR dwReserved[4]" ; &lt;=== Problème!
    Local $structParam = DllStructCreate($videohdr_tag, $param1)
    Local $lpDatas = DllStructGetData($structParam, 1)
    Local $size = DllStructGetData($structParam, 2)
    Local $structPixels = DllStructCreate("BYTE datas[" &amp; $size &amp; "]", $lpDatas)
    Local $Pixels = DllStructGetData($structPixels, 1)
    Local $structImage = _WebcamGetVideoFormat($camera)
    Local $stringBitmap = CreateBmpByStructBITMAPINFOAndPixels($structImage, $Pixels) ;  &lt;----- Peut être enregistré dans un fichier
;~  Local $file = FileOpen(@DesktopDir &amp; "ttest.bmp", 18)
;~  FileWrite($file, $stringBitmap)
;~  FileClose($file)
    Local $Bitmap = _BinaryString2Bitmap($stringBitmap) ;              &lt;----- Objet bitmap exploitable
    Local $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($Bitmap)
    _WinAPI_DeleteObject(GUICtrlSendMsg($Frame, 0x0172, 0, $hBitmap))
EndFunc

Func quit()
    _GDIPlus_Shutdown()
    Exit
EndFunc

PixelsToBmp.au3

Func CreateBmpByStructBITMAPINFOAndPixels($structBITMAPINFO, $Pixels)
    Local $NULL = StringLeft(BinaryToString(0), 1)
    Local $EntetreBMP = "BM"
    Local $Size = $NULL &amp; $NULL &amp; $NULL &amp; $NULL
    Local $ChampReserve = $NULL &amp; $NULL &amp; $NULL &amp; $NULL
    Local $PosDebutImage = $NULL &amp; $NULL &amp; $NULL &amp; $NULL

    Local $NumberCompression = DllStructGetData($structBITMAPINFO, 6)
    $NumberCompression = 0 ; On force le 0 pour avoir le pseudo rendu

    Local $TailleEnteteImage = LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 1), 8))
    Local $TailleImageW = LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 2), 8))
    Local $TailleImageH = LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 3), 8))
    Local $NbPlan = LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 4), 4))
    Local $Profondeur = LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 5), 4))
    Local $Compression =  LittleEndian(Hex($NumberCompression, 8))
    Local $TailleImage =  LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 7), 8))
    Local $ResolutionHoriz = LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 8), 8))
    Local $ResolutionVert = LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 9), 8))
    Local $NbCouleurPalette = LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 10), 8))
    Local $NbCouleurImportantes = LittleEndian(Hex(DllStructGetData($structBITMAPINFO, 11), 8))

    $PosDebutImage =    BinaryLen($EntetreBMP) + _
                            BinaryLen($Size) + _
                            BinaryLen($ChampReserve) + _
                            BinaryLen($PosDebutImage) + _
                            BinaryLen($TailleEnteteImage) + _
                            BinaryLen($TailleImageW) + _
                            BinaryLen($TailleImageH) + _
                            BinaryLen($NbPlan) + _
                            BinaryLen($Profondeur) + _
                            BinaryLen($Compression) + _
                            BinaryLen($TailleImage) + _
                            BinaryLen($ResolutionHoriz) + _
                            BinaryLen($ResolutionVert) + _
                            BinaryLen($NbCouleurPalette) + _
                            BinaryLen($NbCouleurImportantes)

    Local $TailleTotal = $PosDebutImage + BinaryLen($Pixels)
    $PosDebutImage = LittleEndian(Hex($PosDebutImage, 8))
    $Size = LittleEndian(Hex($TailleTotal, 8))
    Local $Retour = $EntetreBMP &amp; _
                    $Size &amp; _
                    $ChampReserve &amp; _
                    $PosDebutImage &amp; _
                    $TailleEnteteImage &amp; _
                    $TailleImageW &amp; _
                    $TailleImageH &amp; _
                    $NbPlan &amp; _
                    $Profondeur &amp; _
                    $Compression &amp; _
                    $TailleImage &amp; _
                    $ResolutionHoriz &amp; _
                    $ResolutionVert &amp; _
                    $NbCouleurPalette &amp; _
                    $NbCouleurImportantes &amp; BinaryToString($Pixels)
    Return  $Retour

EndFunc

Func LittleEndian($String)
    Local $Retour = ""
    Local $Taille = StringLen($String)
    If(Mod($Taille, 2) = 0) Then
        For $i = 0 To $Taille-1 Step 2
            $Retour &amp;= StringLeft(BinaryToString(Number("0x" &amp; StringMid($String, $Taille-($i+1), 2))), 1)
        Next
    EndIf
    Return $Retour
EndFunc

Webcam.au3

#include <structureconstants.au3>
#include <windowsconstants.au3>
#include-once
Global $WM_CAP_START = 0x400
Global $WM_CAP_UNICODE_START = $WM_CAP_START +100
Global $WM_CAP_PAL_SAVEA = $WM_CAP_START + 81
Global $WM_CAP_PAL_SAVEW = $WM_CAP_UNICODE_START + 81
Global $WM_CAP_UNICODE_END = $WM_CAP_PAL_SAVEW
Global $WM_CAP_ABORT = $WM_CAP_START + 69
Global $WM_CAP_DLG_VIDEOCOMPRESSION = $WM_CAP_START + 46
Global $WM_CAP_DLG_VIDEODISPLAY = $WM_CAP_START + 43
Global $WM_CAP_DLG_VIDEOFORMAT = $WM_CAP_START + 41
Global $WM_CAP_DLG_VIDEOSOURCE = $WM_CAP_START + 42
Global $WM_CAP_DRIVER_CONNECT = $WM_CAP_START + 10
Global $WM_CAP_DRIVER_DISCONNECT = $WM_CAP_START + 11
Global $WM_CAP_DRIVER_GET_CAPS = $WM_CAP_START + 14
Global $WM_CAP_DRIVER_GET_NAMEA = $WM_CAP_START + 12
Global $WM_CAP_DRIVER_GET_NAMEW = $WM_CAP_UNICODE_START + 12
Global $WM_CAP_DRIVER_GET_VERSIONA = $WM_CAP_START + 13
Global $WM_CAP_DRIVER_GET_VERSIONW = $WM_CAP_UNICODE_START + 13
Global $WM_CAP_EDIT_COPY = $WM_CAP_START + 30
Global $WM_CAP_END = $WM_CAP_UNICODE_END
Global $WM_CAP_FILE_ALLOCATE = $WM_CAP_START + 22
Global $WM_CAP_FILE_GET_CAPTURE_FILEA = $WM_CAP_START + 21
Global $WM_CAP_FILE_GET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 21
Global $WM_CAP_FILE_SAVEASA = $WM_CAP_START + 23
Global $WM_CAP_FILE_SAVEASW = $WM_CAP_UNICODE_START + 23
Global $WM_CAP_FILE_SAVEDIBA = $WM_CAP_START + 25
Global $WM_CAP_FILE_SAVEDIBW = $WM_CAP_UNICODE_START + 25
Global $WM_CAP_FILE_SET_CAPTURE_FILEA = $WM_CAP_START + 20
Global $WM_CAP_FILE_SET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 20
Global $WM_CAP_FILE_SET_INFOCHUNK = $WM_CAP_START + 24
Global $WM_CAP_GET_AUDIOFORMAT = $WM_CAP_START + 36
Global $WM_CAP_GET_CAPSTREAMPTR = $WM_CAP_START + 1
Global $WM_CAP_GET_MCI_DEVICEA = $WM_CAP_START + 67
Global $WM_CAP_GET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 67
Global $WM_CAP_GET_SEQUENCE_SETUP = $WM_CAP_START + 65
Global $WM_CAP_GET_STATUS = $WM_CAP_START + 54
Global $WM_CAP_GET_USER_DATA = $WM_CAP_START + 8
Global $WM_CAP_GET_VIDEOFORMAT = $WM_CAP_START + 44
Global $WM_CAP_GRAB_FRAME = $WM_CAP_START + 60
Global $WM_CAP_GRAB_FRAME_NOSTOP = $WM_CAP_START + 61
Global $WM_CAP_PAL_AUTOCREATE = $WM_CAP_START + 83
Global $WM_CAP_PAL_MANUALCREATE = $WM_CAP_START + 84
Global $WM_CAP_PAL_OPENA = $WM_CAP_START + 80
Global $WM_CAP_PAL_OPENW = $WM_CAP_UNICODE_START + 80
Global $WM_CAP_PAL_PASTE = $WM_CAP_START + 82
Global $WM_CAP_SEQUENCE = $WM_CAP_START + 62
Global $WM_CAP_SEQUENCE_NOFILE = $WM_CAP_START + 63
Global $WM_CAP_SET_AUDIOFORMAT = $WM_CAP_START + 35
Global $WM_CAP_SET_CALLBACK_CAPCONTROL = $WM_CAP_START + 85
Global $WM_CAP_SET_CALLBACK_ERRORA = $WM_CAP_START + 2
Global $WM_CAP_SET_CALLBACK_ERRORW = $WM_CAP_UNICODE_START + 2
Global $WM_CAP_SET_CALLBACK_FRAME = $WM_CAP_START + 5
Global $WM_CAP_SET_CALLBACK_STATUSA = $WM_CAP_START + 3
Global $WM_CAP_SET_CALLBACK_STATUSW = $WM_CAP_UNICODE_START + 3
Global $WM_CAP_SET_CALLBACK_VIDEOSTREAM = $WM_CAP_START + 6
Global $WM_CAP_SET_CALLBACK_WAVESTREAM = $WM_CAP_START + 7
Global $WM_CAP_SET_CALLBACK_YIELD = $WM_CAP_START + 4
Global $WM_CAP_SET_MCI_DEVICEA = $WM_CAP_START + 66
Global $WM_CAP_SET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 66
Global $WM_CAP_SET_OVERLAY = $WM_CAP_START + 51
Global $WM_CAP_SET_PREVIEW = $WM_CAP_START + 50
Global $WM_CAP_SET_PREVIEWRATE = $WM_CAP_START + 52
Global $WM_CAP_SET_SCALE = $WM_CAP_START + 53
Global $WM_CAP_SET_SCROLL = $WM_CAP_START + 55
Global $WM_CAP_SET_SEQUENCE_SETUP = $WM_CAP_START + 64
Global $WM_CAP_SET_USER_DATA = $WM_CAP_START + 9
Global $WM_CAP_SET_VIDEOFORMAT = $WM_CAP_START + 45
Global $WM_CAP_SINGLE_FRAME = $WM_CAP_START + 72
Global $WM_CAP_SINGLE_FRAME_CLOSE = $WM_CAP_START + 71
Global $WM_CAP_SINGLE_FRAME_OPEN = $WM_CAP_START + 70
Global $WM_CAP_STOP = $WM_CAP_START + 68
Global $SAVE_FILE = @ScriptDir &amp; "snapshot.bmp"
Global $ptrCallBack = -1
Local $avi = DllOpen("avicap32.dll")
Local $WM_CAP_USER_DLL = DllOpen("user32.dll")

;===============================================================================
;
; Description:    Open's a webcam preview screen in your gui
; Syntax:          _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight)
; Parameter(s):  $sHwnd  - The handle of the gui
;                  $sLeft    - Left coord. of the preview screen
;                  $sTop      - Top coord. of the preview screen
;                  $sWidth  - Width of the preview screen
;                  $sHeight   - Height of the preview screen
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns id needed for other controls
;                  On Failure - Returns -1
; Author(s):        Ludocus
; Note(s):        None
;
;===============================================================================
func _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight, $index = 0)
    Local $cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,$WS_VISIBLE), "int", $sLeft, "int", $sTop, "int", $sWidth, "int", $sHeight, "hwnd", $sHwnd, "int", 1)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_DRIVER_CONNECT, "int", $index, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_SCALE, "int", 1, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_OVERLAY, "int", 1, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEW, "int", 1, "int", 0)
;~  DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEWRATE, "int", 1, "int", 0)
    if @error then return -1
    return $cap[0]
EndFunc

;~ Func _WebcamListCam($sId
;===============================================================================
;
; Description:    Creates a Snapshot from a webcam
; Syntax:          _WebcamSnap($sId, $sFile)
; Parameter(s):  $sId      - Id (returned from _WebcamOpen)
;                  $sFile    - File to save the snapshot to (*.bmp)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                  On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):        None
;
;===============================================================================
Func _WebcamSnap($sId, $sFile = $SAVE_FILE)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GRAB_FRAME, "int", 0, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_FILE_SAVEDIBA, "int", 0, "str", $sFile)
    if @error Then
        return 0
    Else
        return 1
    EndIf
EndFunc

;===============================================================================
;
; Description:    Closes the preview screen created with _WebcamOpen
; Syntax:          _WebcamClose($sId)
; Parameter(s):  $sId      - Id (returned from _WebcamOpen)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                  On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):        None
;
;===============================================================================
Func _WebcamClose($sId)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_END, "int", 0, "int", 0)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_DRIVER_DISCONNECT, "int", 0, "int", 0)
    DllClose($WM_CAP_USER_DLL)
    if @error Then
        return 0
    Else
        return 1
    EndIf
EndFunc

;===============================================================================
;
; Description:    Starts recording the webcam to a file
; Syntax:          _WebcamRecordStart($sFile, $sId)
; Parameter(s):  $sId      - Id (returned from _WebcamOpen)
;                  $sFile    - File to save the movie to (*.avi)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                  On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):        Stop recording by: _WebcamRecordStop($Id)
;
;===============================================================================
Func _WebcamRecordStart($sFile, $sId)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_FILE_SET_CAPTURE_FILEA, "int", 0, "str", $sFile)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SEQUENCE, "int", 0, "int", 0)
    if @error Then
        return 0
    Else
        return 1
    EndIf

EndFunc

;===============================================================================
;
; Description:    Stops recording.
; Syntax:          _WebcamRecordStop($sId)
; Parameter(s):  $sId      - Id (returned from _WebcamOpen)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                  On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):        None
;
;===============================================================================
Func _WebcamRecordStop($sId)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_STOP, "int", 0, "int", 0)
    if @error Then
        return 0
    Else
        return 1
    EndIf
EndFunc ;==&gt; _WebcamRecordStop

Func _WebCamGetNumberOfDevices()
    Local $Devices = _WebCamGetDevicesDescription()
    Return $Devices[0][0]
EndFunc

Func _WebCamGetDevicesDescription()
    Local $compteur = 0
    Local $Devices[1][3] = [[0, 0, 0]]
    Local $DeviceTemp
    While(True)
        $DeviceTemp = _WebCamGetDeviceDescription($compteur)
        If(@error) Then ExitLoop
        $Devices[0][0] += 1
        ReDim $Devices[$Devices[0][0]+1][UBound($Devices, 2)]
        $Devices[$Devices[0][0]][0] = $compteur
        $Devices[$Devices[0][0]][1] = $DeviceTemp[0]
        $Devices[$Devices[0][0]][2] = $DeviceTemp[1]
        $compteur += 1
    WEnd
    Return $Devices
EndFunc

Func _WebCamGetDeviceDescription($iIndex)
    Local $tBuffName, $tBuffVer
    Local $aResult, $aRet[2]

    $tBuffName = DllStructCreate("char[128]")
    $tBuffVer  = DllStructCreate("char[128]")

    $aResult = DllCall("avicap32.dll", "int", "capGetDriverDescriptionA", "ushort", $iIndex, "ptr", DllStructGetPtr($tBuffName), _
                        "int", 128, "ptr", DllStructGetPtr($tBuffVer), "int", 128)

    If @error Then Return SetError(@error, @extended, 0)

    If $aResult[0] Then
        $aRet[0] = DllStructGetData($tBuffName, 1)
        $aRet[1] = DllStructGetData($tBuffVer, 1)
        Return $aRet
    EndIf

    Return SetError(1, 0, 0)
EndFunc

Func _WebcamGetFrame($sId)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GRAB_FRAME, "int", 0, "int", 0)
EndFunc

Func _WebcamSetCallBackFram($sId, $func)
    If($ptrCallBack &lt;&gt; -1) Then
        DllCallbackFree($ptrCallBack)
    EndIf
    $CallBack   = DllCallbackRegister($func, "long", "hwnd;ptr")
    $ptrCallBack = DllCallbackGetPtr($CallBack)
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_CALLBACK_FRAME, "int", 0, "lparam", $ptrCallBack)
EndFunc

Func _WebcamStopCallBackFram($sId)
    DllCallbackFree($ptrCallBack)
    $ptrCallBack = -1
    DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_CALLBACK_FRAME, "int", 0, "lparam", 0)
EndFunc

Func _WebcamGetVideoFormat($sId)
    Local $structVideo = DllStructCreate($tagBITMAPINFO)
    Local $Temp = DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GET_VIDEOFORMAT, "int", DllStructGetSize($structVideo), "int", DllStructGetPtr($structVideo))
    Return $structVideo
EndFunc

Func _WebcamGetResolution($sId)
    Local $structVideo = DllStructCreate($tagBITMAPINFO)
    Local $Temp = DllCall($WM_CAP_USER_DLL, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GET_VIDEOFORMAT, "int", DllStructGetSize($structVideo), "int", DllStructGetPtr($structVideo))
    Local $Taille[2] = [DllStructGetData($structVideo, 2), DllStructGetData($structVideo, 3)]
    Return $Taille
EndFunc

Func _WebcamDisplayVideoFormat($sId)
    Local $structBITMAPINFO = _WebcamGetVideoFormat($sId)
    MsgBox(0, "",   DllStructGetData($structBITMAPINFO, 1) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 2) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 3) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 4) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 5) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 6) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 7) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 8) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 9) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 10) &amp; @CRLF &amp; _
                    DllStructGetData($structBITMAPINFO, 11))
EndFunc

Bitmap_String.au3

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.6.1
Author:      Matwachich, Thanks UEZ!

Script Function:
    Simple converting between bitmaps and binary string

#ce ----------------------------------------------------------------------------

#include <gdiplus.au3>
#include <memory.au3>

Global Const $InterpolationModeInvalid = -1
Global Const $InterpolationModeDefault = 0
Global Const $InterpolationModeLowQuality = 1
Global Const $InterpolationModeHighQuality = 2
Global Const $InterpolationModeBilinear = 3
Global Const $InterpolationModeBicubic = 4
Global Const $InterpolationModeNearestNeighbor = 5
Global Const $InterpolationModeHighQualityBilinear = 6
Global Const $InterpolationModeHighQualityBicubic = 7

; ##############################################################

Func _BinaryString2Bitmap($pic)
    ;thanks to ProgAndy for mem allocation lines (now thahanks UEZ for memory leak correction)
    ;   http://www.autoitscript.com/forum/topic/132103-udf-g-engin/page__view__findpost__p__920873
    Local $declared = True
    If Not $ghGDIPDll Then
        _GDIPlus_Startup()
        $declared = False
    EndIf
    ; ---
    Local $memBitmap, $len, $tMem, $hImage, $hData, $pData, $hStream, $hBitmapFromStream
    $memBitmap = Binary($pic) ;load image  saved in variable (memory) and convert it to binary
    $len = BinaryLen($memBitmap) ;get length of image

    $hData  = _MemGlobalAlloc($len, $GMEM_MOVEABLE) ;allocates movable memory  ($GMEM_MOVEABLE = 0x0002)
    $pData = _MemGlobalLock($hData)  ;translate the handle into a pointer
    $tMem =  DllStructCreate("byte[" &amp; $len &amp; "]", $pData) ;create struct
    DllStructSetData($tMem, 1, $memBitmap) ;fill struct with image data
    _MemGlobalUnlock($hData) ;decrements the lock count  associated with a memory object that was allocated with GMEM_MOVEABLE

    $hStream = _WinAPI_CreateStreamOnHGlobal($pData) ;Creates a stream object that uses an HGLOBAL memory handle to store the stream contents
    $hBitmapFromStream = _GDIPlus_BitmapCreateFromStream($hStream) ;Creates a Bitmap object based on an IStream COM interface

    Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
    Local $aCall = DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "dword", 8 + 8 * @AutoItX64, "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT))
    $tMem = 0

    If Not $declared Then _GDIPlus_Shutdown()

    Return $hBitmapFromStream
EndFunc

;GDIPlus.au3 must be included; JPG quality: 0 - 100 (worst - best)
;$Bitmap must be a GDI+ compatible bitmap format!!!
Func _Bitmap2BinaryString($Bitmap, $JPEG_Quality = 90) ;code by Andreik, modified by UEZ
    Local $declared = True
    If Not $ghGDIPDll Then
        _GDIPlus_Startup()
        $declared = False
    EndIf
    ; ---
    Local $STREAM = DllCall("ole32.dll", "uint", "CreateStreamOnHGlobal", "ptr", 0, "bool", 1, "ptr*", 0)
    $STREAM = $STREAM[3]
    Local $JPG_ENCODER = _GDIPlus_EncodersGetCLSID("JPG")
    Local $TAG_ENCODER = _WinAPI_GUIDFromString($JPG_ENCODER)
    Local $PTR_ENCODER = DllStructGetPtr($TAG_ENCODER)
    Local $tParams = _GDIPlus_ParamInit (1)
    Local $tData = DllStructCreate("int Quality")
    DllStructSetData($tData, "Quality", $JPEG_Quality)
    Local $pData = DllStructGetPtr($tData)
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData)
    Local $pParams = DllStructGetPtr($tParams)
    DllCall($ghGDIPDll, "uint", "GdipSaveImageToStream", "ptr", $Bitmap, "ptr", $STREAM, "ptr", $PTR_ENCODER, "ptr", $pParams)
    $tData = 0
    $tParams = 0
    Local $MEMORY = DllCall("ole32.dll", "uint", "GetHGlobalFromStream", "ptr", $STREAM, "ptr*", 0)
    $MEMORY = $MEMORY[2]
    Local $MEM_SIZE = _MemGlobalSize($MEMORY)
    Local $MEM_PTR = _MemGlobalLock($MEMORY)
    Local $DATA_STRUCT = DllStructCreate("byte[" &amp; $MEM_SIZE &amp; "]", $MEM_PTR)
    Local $DATA = DllStructGetData($DATA_STRUCT, 1)
    Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
    Local $aCall = DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $STREAM, "dword", 8 + 8 * @AutoItX64, "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT))
    _MemGlobalFree($MEMORY)
    ; ---
    If Not $declared Then _GDIPlus_Shutdown()
    Return $DATA
EndFunc

; ##############################################################

Func _GDIPlus_HBitmapToBitmap($hbitmap, $iWidth, $iHeight, $Quality = $InterpolationModeHighQualityBicubic)
    $bitmap = _GDIPlus_BitmapCreateFromHBITMAP($hbitmap)
    ToolTip($bitmap)
    ; ---
    $graphics = _GDIPlus_ImageGetGraphicsContext($bitmap)
    $resizedbitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $graphics)
    $graphics2 = _GDIPlus_ImageGetGraphicsContext($resizedbitmap)
    _GDIPLUS_GraphicsSetInterpolationMode($graphics2, $Quality)
    _GDIPlus_GraphicsDrawImageRect($graphics2, $bitmap, 0, 0, $iWidth, $iHeight)
    ;_GDIPlus_ImageSaveToFile($resizedbitmap, @ScriptDir &amp; "test.jpg")
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_GraphicsDispose($graphics2)
    _GDIPlus_BitmapDispose($bitmap)
   ; _GDIPlus_BitmapDispose($resizedbitmap)
   Return $resizedbitmap
EndFunc  ;==&gt;_SavehBitmap

Func _GDIPLUS_GraphicsSetInterpolationMode($hGraphics, $iMode)
    DllCall($ghGDIPDll, "int", "GdipSetInterpolationMode", "hwnd", $hGraphics,"int",$iMode)
EndFunc  ;==&gt;_GDIPLUS_GraphicsSetInterpolationMode

Func _WinAPI_CreateStreamOnHGlobal($hGlobal = 0, $fDeleteOnRelease = True)
    Local $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "hwnd", $hGlobal, "int", $fDeleteOnRelease, "ptr*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[3]
EndFunc   ;==&gt;_WinAPI_CreateStreamOnHGlobal

Func _GDIPlus_BitmapCreateFromStream($pStream)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $pStream, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[2]
EndFunc   ;==&gt;_GDIPlus_BitmapCreateFromStream

Sorry for my english :)

Edited by TommyDDR
_GUIRegisterMsg (Register more than 1 time the same Msg), _Resize_Window (GUICtrlSetResizing for children windows), _GUICtrlSetOnHover (Link a function when mouse go on, left, clic down, clic up, on a control), _InputHeure (Create an input for hour contain), _GUICtrlCalendar (Make a complete calendar), _GUICtrlCreateGraphic3D (Create a 3D graph), _ArrayEx.au3 (Array management), _GUIXViewEx.au3 (List/Tree View management).

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...