GDIPlus_ImageLoadFromInet (without Saving)

  • So, wie die Überschrift schon sagt, die folgende Funktion erlaubt es ein Bild aus dem Internet zu laden(auch Bilder von durch Passwort geschützte Websiten, da es über TCP geht) und gibt dann ohne zu speichern den Handle des Images (GDI+) zurück.
    Schaut es euch an und postet euer Meinung...Ich freu mich schon darauf :thumbup:

    Spoiler anzeigen
    [autoit]

    #include
    #include
    #Include
    #include
    #NoTrayIcon
    _GDIPlus_Startup()

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

    $hwnd = GUICreate("Display image from memory", 600,100,-1,-1,-1, bitor($WS_EX_LAYERED,$WS_EX_TOOLWINDOW))
    GUISetState()
    WinSetOnTop($hwnd,"",1)
    ;Load Image
    $hImage = _GDIPlus_ImageLoadFromInet( "http://www.autoit.de", "/wcf/images/future/autoit_logo_gtaspider.png")
    ;Show Image
    SetBitmap($hwnd, $hImage, 255)
    ;Exit:
    for $i = 255 to 0 step -1
    SetBitmap($hwnd, $hImage, $i)
    Sleep(20)
    Next
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
    Exit

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

    ;-----------------------------------------------------------------------------------------------------------------
    ; Author: Faweyr
    ; Thanks: Upload Function: @night@ ( http://www.autoitbot.de )
    ; Binary to Image: UEZ ( http://www.autoit.de )
    ; Function: _GDIPlus_LoadWebImage( $sHost, $sSeite)
    ; Parameter: $sHost = Host der Seite (zb. http://www.autoit.de)
    ; $sSeite = Der Link zum Bild ohne den Hostnamen (zb. /wcf/images/future/autoit_logo_gtaspider.png )
    ; Note: Läd ein Bild aus dem Inet und gibt das Handle (GDI+) zurück ohne es als Datei abzuspeichern
    ; Funktioniert auch bei Passwort geschützte Bereiche, da es über TCP funktioniert
    ; Return: Fehler: 0
    ; Erfolg: Handle of Image
    ; Error: 0 = Succes
    ; 1 = Can´t connected
    ; Example: _GDIPlus_ImageLoadFromInet( "http://www.autoit.de", "/wcf/images/future/autoit_logo_gtaspider.png")
    ;-----------------------------------------------------------------------------------------------------------------
    Func _GDIPlus_ImageLoadFromInet( $sHost, $sSeite)
    If StringInStr($sHost, "http://") Then $sHost = StringReplace($sHost, "http://", "")
    If StringRegExp($sHost, "\.../") Then $sHost = Stringreplace($sHost, "/", "")
    TCPStartup()
    $sIp = TCPNameToIP($sHost)

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

    $sData = "GET "&$sSeite&" HTTP/1.1" & @crlf & _
    "Host: " & $sHost & @crlf & _
    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)" & @crlf & _
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & @crlf & _
    "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3" & @crlf & _
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" & @crlf & _
    "Connection: close" & @crlf & @CRLF

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

    $sSocket = TCPConnect($sIp, 80)
    TCPSend($sSocket, $sData)
    If @error Then return Seterror(1)
    Local $sRecv_header = "", $sRecv_binary = "0x"
    Do
    $sRecv_header = TCPRecv($sSocket, 1)
    until $sRecv_header <> ""
    Do
    $sRecv_header &= TCPRecv($sSocket, 1)
    until StringInStr($sRecv_header, @crlf & @crlf)
    $aArray_help = _stringbetween($sRecv_header, 'Content-Length: ', @crlf)
    If NOT isarray($aArray_help) Then
    TCPCloseSocket($sSocket)
    TCPShutdown()
    return Seterror(1)
    EndIf
    $iBytes = $aArray_help[0]
    Do
    $sRecv_binary &= StringTrimLeft(TCPRecv($sSocket, 1024, 1),2)
    Until BinaryLen( Binary($sRecv_binary) ) >= $iBytes
    TCPCloseSocket($sSocket)
    TCPShutdown()
    $memBitmap = Binary($sRecv_binary)
    $len = BinaryLen($memBitmap)
    $hData = _MemGlobalAlloc($len, 0x0002)
    $pData = _MemGlobalLock($hData)
    $tMem = DllStructCreate("byte[" & $len & "]", $pData)
    DllStructSetData($tMem, 1, $memBitmap)
    _MemGlobalUnlock($hData)
    $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "hwnd", $pData, "int", True, "ptr*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $aResult[3], "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[2]
    EndFunc

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

    Func SetBitmap($hGUI, $hImage, $iOpacity)
    Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend

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

    $hScrDC = _WinAPI_GetDC(0)
    $hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
    $hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    $hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize )
    DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth ($hImage))
    DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha" , $iOpacity )
    DllStructSetData($tBlend, "Format", 1)
    _WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
    _WinAPI_ReleaseDC (0, $hScrDC)
    _WinAPI_SelectObject($hMemDC, $hOld)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC ($hMemDC)
    EndFunc

    [/autoit]

    Einmal editiert, zuletzt von Faweyr (27. April 2010 um 16:39)

  • Pfad(62,31) : WARNING: $socket: possibly used before declaration.
    TCPCloseSocket($socket)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    Pfad(62,31) : ERROR: $socket: undeclared global variable.
    TCPCloseSocket($socket)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    Pfad - 1 error(s), 1 warning(s)

    Wär auch nett wenns funnktioniert, mein Code ist dieser:

    [autoit]

    $hImage = _GDIPlus_ImageLoadFromInet( $GUI_url, "/favicon.ico")
    $Pic1 = GUICtrlCreatePic($hImage, 56, 8, 17, 17, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))

    [/autoit]
  • In der Funktion von Faweyr muss es in Zeile 66 bestimmt nicht:

    TCPCloseSocket($socket)

    sondern

    TCPCloseSocket($sSocket)

    heißen.

    Zur Nutzung dieses Forum's, ist ein Übersetzer für folgende Begriffe unerlässlich:

    "On-Bort, weier, verscheiden, schädliges, Butten steyling, näckstet, Parr, Porblem, scripe, Kompletenz, harken, manuel zu extramieren, geckukt, würglich, excell, acces oder Compilevorgeng"

  • ProblemUser:
    Jedoch kann dein Code nicht funktionieren, da ein ImageHandle (GDI+) zurückgegeben wird und .ico auch kein Image sonder ein Icon ist.
    Wenn du ein Image mit "GUICtrlCreatePic" anzeigen willst, dann verwende:

    Spoiler anzeigen
    [autoit]

    Func _SetBitmapToCtrl($CtrlId, $hBitmap)
    Local Const $STM_SETIMAGE = 0x0172
    Local Const $STM_GETIMAGE = 0x0173
    Local Const $BM_SETIMAGE = 0xF7
    Local Const $BM_GETIMAGE = 0xF6
    Local Const $IMAGE_BITMAP = 0
    Local Const $SS_BITMAP = 0x0E
    Local Const $BS_BITMAP = 0x0080
    Local Const $GWL_STYLE = -16

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

    Local $hWnd, $hPrev, $Style, $iCtrl_SETIMAGE, $iCtrl_GETIMAGE, $iCtrl_BITMAP

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

    ; determine control class and adjust constants accordingly
    Switch _WinAPI_GetClassName($CtrlId)
    Case "Button" ; button,checkbox,radiobutton,groupbox
    $iCtrl_SETIMAGE = $BM_SETIMAGE
    $iCtrl_GETIMAGE = $BM_GETIMAGE
    $iCtrl_BITMAP = $BS_BITMAP
    Case "Static" ; picture,icon,label
    $iCtrl_SETIMAGE = $STM_SETIMAGE
    $iCtrl_GETIMAGE = $STM_GETIMAGE
    $iCtrl_BITMAP = $SS_BITMAP
    Case Else
    Return SetError(1, 0, 0)
    EndSwitch

    $hWnd = GUICtrlGetHandle($CtrlId)
    If $hWnd = 0 Then Return SetError(2, 0, 0)

    ; set SS_BITMAP/BS_BITMAP style to the control
    $Style = _WinAPI_GetWindowLong($hWnd, $GWL_STYLE)
    If @error Then Return SetError(3, 0, 0)
    _WinAPI_SetWindowLong($hWnd, $GWL_STYLE, BitOR($Style, $iCtrl_BITMAP))
    If @error Then Return SetError(4, 0, 0)

    ; set image to the control
    $hPrev = _SendMessage($hWnd, $iCtrl_SETIMAGE, $IMAGE_BITMAP, $hBitmap)
    If @error Then Return SetError(5, 0, 0)
    If $hPrev Then _WinAPI_DeleteObject($hPrev)

    Return 1
    EndFunc

    [/autoit]
  • Hab ich auch mal vor langer Zeit versucht, allerdings wusste ich nicht, wie ich es umsetzen könnte
    (kannte/konnte damals noch kein TCP, und GDI+ auch noch nicht so richtig)

    Wirklich super! :thumbup:
    Werds bald testen!

  • vllt bin ich ja blind aber:

    [autoit]

    #include
    #include
    #Include
    #include

    [/autoit]

    muss zu

    [autoit]

    #include <GDIPlus.au3>
    #include <Memory.au3>
    #Include <String.au3>
    #include <StructureConstants.au3>
    #include <WinAPI.au3>
    #include <WindowsConstants.au3>

    [/autoit]
  • das is n kleiner bug der hin und wieder beim kopieren entsteht.
    keine sorge, bist nicht blind ;)