Binary auf GUI zeichnen lassen

  • Hey,

    ich möchte gerne Bilder aus dem Netz auf meine GUI zeichnen lassen und finde es unsinnig, wenn ich die als binary bekomme, die erst abzuspeichern...
    UEZ hat mir da schon mit seiner UDF geholfen, leider habe ich nie so das rechte Verständnis für GDI+...
    Wäre sehr dankbar, wenn mir jemand den letzten Step erklärt :).
    Lauffähiger Minimalcode:

    Spoiler anzeigen
    [autoit]

    #include <winhttp.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>
    #include <Memory.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global $hSession, $hConnect
    Global $bPic, $hFile

    [/autoit] [autoit][/autoit] [autoit]

    $hSession = _WinHttpOpen("ShowCase")
    $hConnect = _WinHttpConnect($hSession, "bilder.fotoweinwurm.at")

    [/autoit] [autoit][/autoit] [autoit]

    $bPic = _WinHttpSimpleRequest($hConnect, "GET", "/archive/w/hp281309.jpg", Default, Default, Default, Default, 2)

    [/autoit] [autoit][/autoit] [autoit]

    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)

    [/autoit] [autoit][/autoit] [autoit]

    $GUI = GUICreate("ShowCase")
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($GUI)
    $pic = Load_BMP_From_Mem($bPic, True)
    $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($pic)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage2, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()

    [/autoit] [autoit][/autoit] [autoit]

    If $msg = $GUI_EVENT_CLOSE Then
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    ExitLoop
    EndIf
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    ;======================================================================================
    ; Function Name: Load_BMP_From_Mem
    ; Description: Loads an image which is saved as a binary string and converts it to a bitmap or hbitmap
    ;
    ; Parameters: $bImage: the binary string which contains any valid image which is supported by GDI+
    ; Optional: $hHBITMAP: if false a bitmap will be created, if true a hbitmap will be created
    ;
    ; Remark: hbitmap format is used generally for GUI internal images, $bitmap is more a GDI+ image format
    ; Don't forget _GDIPlus_Startup() and _GDIPlus_Shutdown()
    ;
    ; Requirement(s): GDIPlus.au3, Memory.au3 and _GDIPlus_BitmapCreateDIBFromBitmap() from WinAPIEx.au3
    ; Return Value(s): Success: handle to bitmap (GDI+ bitmap format) or hbitmap (WinAPI bitmap format),
    ; Error: 0
    ; Error codes: 1: $bImage is not a binary string
    ; 2: unable to create stream on HGlobal
    ; 3: unable to create bitmap from stream
    ;
    ; Author(s): UEZ
    ; Additional Code: thanks to progandy for the MemGlobalAlloc and tVARIANT lines and
    ; Yashied for _GDIPlus_BitmapCreateDIBFromBitmap() from WinAPIEx.au3
    ; Version: v0.97 Build 2012-04-10 Beta
    ;=======================================================================================
    Func Load_BMP_From_Mem($bImage, $hHBITMAP = False)
    If Not IsBinary($bImage) Then Return SetError(1, 0, 0)
    Local $aResult
    Local Const $memBitmap = Binary($bImage) ;load image saved in variable (memory) and convert it to binary
    Local Const $len = BinaryLen($memBitmap) ;get length of image
    Local Const $hData = _MemGlobalAlloc($len, $GMEM_MOVEABLE) ;allocates movable memory ($GMEM_MOVEABLE = 0x0002)
    Local Const $pData = _MemGlobalLock($hData) ;translate the handle into a pointer
    Local $tMem = DllStructCreate("byte[" & $len & "]", $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
    $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $pData, "int", True, "ptr*", 0) ;Creates a stream object that uses an HGLOBAL memory handle to store the stream contents
    If @error Then Return SetError(2, 0, 0)
    Local Const $hStream = $aResult[3]
    $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) ;Creates a Bitmap object based on an IStream COM interface
    If @error Then Return SetError(3, 0, 0)
    Local Const $hBitmap = $aResult[2]
    Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
    DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "dword", 8 + 8 * @AutoItX64, _
    "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT)) ;release memory from $hStream to avoid memory leak
    $tMem = 0
    $tVARIANT = 0
    If $hHBITMAP Then
    Local Const $hHBmp = _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
    _GDIPlus_BitmapDispose($hBitmap)
    Return $hHBmp
    EndIf
    Return $hBitmap
    EndFunc ;==>Load_BMP_From_Mem

    [/autoit] [autoit][/autoit] [autoit]

    Func _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
    Local $tBIHDR, $Ret, $tData, $pBits, $hResult = 0
    $Ret = DllCall($ghGDIPDll, 'uint', 'GdipGetImageDimension', 'ptr', $hBitmap, 'float*', 0, 'float*', 0)
    If (@error) Or ($Ret[0]) Then Return 0
    $tData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $Ret[2], $Ret[3], $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    $pBits = DllStructGetData($tData, 'Scan0')
    If Not $pBits Then Return 0
    $tBIHDR = DllStructCreate('dword;long;long;ushort;ushort;dword;dword;long;long;dword;dword')
    DllStructSetData($tBIHDR, 1, DllStructGetSize($tBIHDR))
    DllStructSetData($tBIHDR, 2, $Ret[2])
    DllStructSetData($tBIHDR, 3, $Ret[3])
    DllStructSetData($tBIHDR, 4, 1)
    DllStructSetData($tBIHDR, 5, 32)
    DllStructSetData($tBIHDR, 6, 0)
    $hResult = DllCall('gdi32.dll', 'ptr', 'CreateDIBSection', 'hwnd', 0, 'ptr', DllStructGetPtr($tBIHDR), 'uint', 0, 'ptr*', 0, 'ptr', 0, 'dword', 0)
    If (Not @error) And ($hResult[0]) Then
    DllCall('gdi32.dll', 'dword', 'SetBitmapBits', 'ptr', $hResult[0], 'dword', $Ret[2] * $Ret[3] * 4, 'ptr', DllStructGetData($tData, 'Scan0'))
    $hResult = $hResult[0]
    Else
    $hResult = 0
    EndIf
    _GDIPlus_BitmapUnlockBits($hBitmap, $tData)
    Return $hResult
    EndFunc ;==>_GDIPlus_BitmapCreateDIBFromBitmap

    [/autoit] [autoit][/autoit] [autoit]

    Func __GDIPlus_Shutdown()
    _GDIPlus_Shutdown()
    EndFunc ;==>GDIPlus_Shutdown

    [/autoit]

    LG
    Aca

    2 Mal editiert, zuletzt von Acanis (26. September 2013 um 22:17)

  • Guckst du hier:

    Spoiler anzeigen
    [autoit]


    #include <winhttp.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>
    #include <Memory.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Global $hSession, $hConnect
    Global $bCaptcha, $hFile

    [/autoit] [autoit][/autoit] [autoit]

    $hSession = _WinHttpOpen("ShowCase")
    $hConnect = _WinHttpConnect($hSession, "bilder.fotoweinwurm.at")

    [/autoit] [autoit][/autoit] [autoit]

    $bPic = _WinHttpSimpleRequest($hConnect, "GET", "/archive/w/hp281309.jpg", Default, Default, Default, Default, 2)

    [/autoit] [autoit][/autoit] [autoit]

    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hSession)

    [/autoit] [autoit][/autoit] [autoit]

    $GUI = GUICreate("ShowCase")
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($GUI)
    $hImage = Load_BMP_From_Mem(Binary($bPic))
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()

    [/autoit] [autoit][/autoit] [autoit]

    If $msg = $GUI_EVENT_CLOSE Then
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    ExitLoop
    EndIf
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    ;======================================================================================
    ; Function Name: Load_BMP_From_Mem
    ; Description: Loads an image which is saved as a binary string and converts it to a bitmap or hbitmap
    ;
    ; Parameters: $bImage: the binary string which contains any valid image which is supported by GDI+
    ; Optional: $hHBITMAP: if false a bitmap will be created, if true a hbitmap will be created
    ;
    ; Remark: hbitmap format is used generally for GUI internal images, $bitmap is more a GDI+ image format
    ; Don't forget _GDIPlus_Startup() and _GDIPlus_Shutdown()
    ;
    ; Requirement(s): GDIPlus.au3, Memory.au3 and _GDIPlus_BitmapCreateDIBFromBitmap() from WinAPIEx.au3
    ; Return Value(s): Success: handle to bitmap (GDI+ bitmap format) or hbitmap (WinAPI bitmap format),
    ; Error: 0
    ; Error codes: 1: $bImage is not a binary string
    ; 2: unable to create stream on HGlobal
    ; 3: unable to create bitmap from stream
    ;
    ; Author(s): UEZ
    ; Additional Code: thanks to progandy for the MemGlobalAlloc and tVARIANT lines and
    ; Yashied for _GDIPlus_BitmapCreateDIBFromBitmap() from WinAPIEx.au3
    ; Version: v0.97 Build 2012-04-10 Beta
    ;=======================================================================================
    Func Load_BMP_From_Mem($bImage, $hHBITMAP = False)
    If Not IsBinary($bImage) Then Return SetError(1, 0, 0)
    Local $aResult
    Local Const $memBitmap = Binary($bImage) ;load image saved in variable (memory) and convert it to binary
    Local Const $len = BinaryLen($memBitmap) ;get length of image
    Local Const $hData = _MemGlobalAlloc($len, $GMEM_MOVEABLE) ;allocates movable memory ($GMEM_MOVEABLE = 0x0002)
    Local Const $pData = _MemGlobalLock($hData) ;translate the handle into a pointer
    Local $tMem = DllStructCreate("byte[" & $len & "]", $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
    $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $pData, "int", True, "ptr*", 0) ;Creates a stream object that uses an HGLOBAL memory handle to store the stream contents
    If @error Then Return SetError(2, 0, 0)
    Local Const $hStream = $aResult[3]
    $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0) ;Creates a Bitmap object based on an IStream COM interface
    If @error Then Return SetError(3, 0, 0)
    Local Const $hBitmap = $aResult[2]
    Local $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
    DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "dword", 8 + 8 * @AutoItX64, _
    "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT)) ;release memory from $hStream to avoid memory leak
    $tMem = 0
    $tVARIANT = 0
    If $hHBITMAP Then
    Local Const $hHBmp = _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
    _GDIPlus_BitmapDispose($hBitmap)
    Return $hHBmp
    EndIf
    Return $hBitmap
    EndFunc ;==>Load_BMP_From_Mem

    [/autoit] [autoit][/autoit] [autoit]

    Func _GDIPlus_BitmapCreateDIBFromBitmap($hBitmap)
    Local $tBIHDR, $Ret, $tData, $pBits, $hResult = 0
    $Ret = DllCall($ghGDIPDll, 'uint', 'GdipGetImageDimension', 'ptr', $hBitmap, 'float*', 0, 'float*', 0)
    If (@error) Or ($Ret[0]) Then Return 0
    $tData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $Ret[2], $Ret[3], $GDIP_ILMREAD, $GDIP_PXF32ARGB)
    $pBits = DllStructGetData($tData, 'Scan0')
    If Not $pBits Then Return 0
    $tBIHDR = DllStructCreate('dword;long;long;ushort;ushort;dword;dword;long;long;dword;dword')
    DllStructSetData($tBIHDR, 1, DllStructGetSize($tBIHDR))
    DllStructSetData($tBIHDR, 2, $Ret[2])
    DllStructSetData($tBIHDR, 3, $Ret[3])
    DllStructSetData($tBIHDR, 4, 1)
    DllStructSetData($tBIHDR, 5, 32)
    DllStructSetData($tBIHDR, 6, 0)
    $hResult = DllCall('gdi32.dll', 'ptr', 'CreateDIBSection', 'hwnd', 0, 'ptr', DllStructGetPtr($tBIHDR), 'uint', 0, 'ptr*', 0, 'ptr', 0, 'dword', 0)
    If (Not @error) And ($hResult[0]) Then
    DllCall('gdi32.dll', 'dword', 'SetBitmapBits', 'ptr', $hResult[0], 'dword', $Ret[2] * $Ret[3] * 4, 'ptr', DllStructGetData($tData, 'Scan0'))
    $hResult = $hResult[0]
    Else
    $hResult = 0
    EndIf
    _GDIPlus_BitmapUnlockBits($hBitmap, $tData)
    Return $hResult
    EndFunc ;==>_GDIPlus_BitmapCreateDIBFromBitmap

    [/autoit] [autoit][/autoit] [autoit]

    Func __GDIPlus_Shutdown()
    _GDIPlus_Shutdown()
    EndFunc ;==>GDIPlus_Shutdown

    [/autoit]


    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯