Transparentes GdiPlus-Image mit GDI zeichnen

  • Heute wurde in der Shoutbox quasi danach gefragt, wie man mit GDI ein transparentes GDI+Bild zeichnen kann
    Dies ist möglich mit DrawIcon:

    [autoit]

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

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

    _GDIPlus_Startup()
    Global $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\autoit_logo_gtaspider.png")
    Global $iImageW = _GDIPlus_ImageGetWidth($hImage)
    Global $iImageH = _GDIPlus_ImageGetHeight($hImage)
    Global $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()

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

    Global $hGui = GUICreate("Test", $iImageW, $iImageH)
    GUISetBkColor(0x394E69)
    GUISetState()

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

    Global $hIcon = _WinAPI_CreateIconIndirect($hHBitmap, $hHBitmap)
    _WinAPI_DeleteObject($hHBitmap)

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

    Global $hDC = _WinAPI_GetDC($hGui)
    _WinAPI_DrawIconEx($hDC, 0, 0, $hIcon, $iImageW, $iImageH)
    _WinAPI_DestroyIcon($hIcon)
    _WinAPI_ReleaseDC($hGui, $hDC)

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

    While GUIGetMsg() <> -3
    Sleep(10)
    WEnd

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

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _WinAPI_CreateIconIndirect
    ; Description....: Creates an icon or cursor that has the specified size, colors, and bit patterns.
    ; Syntax.........: _WinAPI_CreateIconIndirect ( $hBitmap, $hMask [, $XHotspot [, $YHotspot [, $fIcon]]] )
    ; Parameters.....: $hBitmap - Handle to the icon color bitmap.
    ; $hMask - Handle to the icon bitmask bitmap.
    ; $XHotspot - Specifies the x-coordinate of a cursor's hot spot. If creates an icon, the hot spot is always in
    ; the center of the icon, and this member is ignored.
    ; $YHotspot - Specifies the y-coordinate of the cursor's hot spot. If creates an icon, the hot spot is always in
    ; the center of the icon, and this member is ignored.
    ; $fIcon - Specifies whether creates an icon or a cursor, valid values:
    ; |TRUE - Creates an icon. (Default)
    ; |FALSE - Creates a cursor.
    ; Return values..: Success - Handle to the icon or cursor that is created.
    ; Failure - 0 and sets the @error flag to non-zero.
    ; Author.........: Yashied
    ; Modified.......:
    ; Remarks........: The system copies the bitmaps before creating the icon or cursor. Because the system may temporarily
    ; select the bitmaps in a device context, $hBitmap and $hMask should not already be selected into a device context.
    ; The application must continue to manage the original bitmaps and delete them by _WinAPI_DeleteObject() when they
    ; are no longer necessary.
    ; Related........:
    ; Link...........: @@MsdnLink@@ CreateIconIndirect
    ; Example........: Yes
    ; ===============================================================================================================================

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

    Func _WinAPI_CreateIconIndirect($hBitmap, $hMask, $XHotspot = 0, $YHotspot = 0, $fIcon = 1)

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

    Local $tICONINFO = DllStructCreate($tagICONINFO)

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

    DllStructSetData($tICONINFO, 1, $fIcon)
    DllStructSetData($tICONINFO, 2, $XHotspot)
    DllStructSetData($tICONINFO, 3, $YHotspot)
    DllStructSetData($tICONINFO, 4, $hMask)
    DllStructSetData($tICONINFO, 5, $hBitmap)

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

    Local $Ret = DllCall('user32.dll', 'ptr', 'CreateIconIndirect', 'ptr', DllStructGetPtr($tICONINFO))

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

    If (@error) Or (Not $Ret[0]) Then
    Return SetError(1, 0, 0)
    EndIf
    Return $Ret[0]
    EndFunc ;==>_WinAPI_CreateIconIndirect

    [/autoit]


    (Das Bild "autoit_logo_gtaspider.png" ist das AutoIt.de-Logo ganz oben)

    E

  • Mit GDI+ gibt es da eine ganz einfach funktion um ein Bild in gewünschter Transparenz zu zeichnen..
    Wenn ich den Hersteller wissen würde, würde ich ihn hier anführen.. Ich entschuldige mich dafür..
    Diese Funktion kommt von Siao (fragt mich nicht wer das ist)

    &quot;Trasparenz setzen&quot;
    [autoit]

    ; #FUNCTION# ===================================================================================================
    ; Name...........: _GDIPlus_GraphicsDrawImageRectRectTrans
    ; Description ...: Draw an Image object with transparency
    ; Syntax.........: _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hImage, $iSrcX, $iSrcY, [$iSrcWidth, _
    ; [$iSrcHeight, [$iDstX, [$iDstY, [$iDstWidth, [$iDstHeight[, [$iUnit = 2]]]]]]])
    ; Parameters ....: $hGraphics - Handle to a Graphics object
    ; $hImage - Handle to an Image object
    ; $iSrcX - The X coordinate of the upper left corner of the source image
    ; $iSrcY - The Y coordinate of the upper left corner of the source image
    ; $iSrcWidth - Width of the source image
    ; $iSrcHeight - Height of the source image
    ; $iDstX - The X coordinate of the upper left corner of the destination image
    ; $iDstY - The Y coordinate of the upper left corner of the destination image
    ; $iDstWidth - Width of the destination image
    ; $iDstHeight - Height of the destination image
    ; $iUnit - Specifies the unit of measure for the image
    ; $nTrans - Value range from 0 (Zero for invisible) to 1.0 (fully opaque)
    ; Return values .: Success - True
    ; Failure - False
    ; Author ........: Siao
    ; Modified.......: Malkey
    ; Remarks .......:
    ; Related .......:
    ; Link ..........; http://www.autoitscript.com/forum/index.ph…ndpost&p=517195
    ; Example .......; Yes
    Func _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth = "", $iSrcHeight = "", _
    $iDstX = "", $iDstY = "", $iDstWidth = "", $iDstHeight = "", $iUnit = 2, $nTrans = 1)
    Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage)
    If $iSrcWidth = 0 Or $iSrcWidth = "" Then $iSrcWidth = $iW
    If $iSrcHeight = 0 Or $iSrcHeight = "" Then $iSrcHeight = $iH
    If $iDstX = "" Then $iDstX = $iSrcX
    If $iDstY = "" Then $iDstY = $iSrcY
    If $iDstWidth = "" Then $iDstWidth = $iSrcWidth
    If $iDstHeight = "" Then $iDstHeight = $iSrcHeight
    If $iUnit = "" Then $iUnit = 2
    ;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    ;blending values:
    $x = DllStructSetData($tColorMatrix, 1, 1, 1) * DllStructSetData($tColorMatrix, 2, 1, 2) * DllStructSetData($tColorMatrix, 3, 1, 3) * _
    DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 1, 5)
    ;;create an image attributes object and update its color matrix
    $hImgAttrib = DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
    $hImgAttrib = $hImgAttrib[1]
    DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr", $hImgAttrib, "int", 1, _
    "int", 1, "ptr", DllStructGetPtr($tColorMatrix), "ptr", 0, "int", 0)
    ;;draw image into graphic object with alpha blend
    DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _
    $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _
    $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0)
    ;;clean up
    DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
    Return
    EndFunc ;==>_GDIPlus_GraphicsDrawImageRectRectTrans
    #EndRegion ;Image Transition

    [/autoit]


    Ich hoffe es war hilfreich..

    Einmal editiert, zuletzt von Schnacko (5. September 2011 um 16:19)

  • Habt ihr das Script auch getestet?
    Es ging um die Frage, wie man ein GDI+Image zu einem HBitmap umwandeln kann, und die Alphawerte erhalten bleiben.
    Das geht, allerdings muss man zum zeichnen DrawIconEx verwenden, denn mit BitBlt wird das nichts.
    Und auch für AlphaBlend müsste man das Bild vorher noch umrechnen - oder?

    Hier hab ich noch eins für euch:

    Spoiler anzeigen
    [autoit]

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

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

    _GDIPlus_Startup()
    Global $hBitmap = _CreateBitmap()
    Global $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()

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

    Global $hGui = GUICreate("Test", 800, 200)
    GUISetBkColor(0x394E69)
    GUISetState()

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

    Global $hIcon = _WinAPI_CreateIconIndirect($hHBitmap, $hHBitmap)
    _WinAPI_DeleteObject($hHBitmap)

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

    Global $hDC = _WinAPI_GetDC($hGui)
    Global $hDC_BG = _WinAPI_CreateCompatibleDC($hDC)
    Global $hBMP_BG = _WinAPI_LoadImage(0, @ScriptDir & "\Wasserlilien.bmp", $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
    _WinAPI_SelectObject($hDC_BG, $hBMP_BG)

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

    _WinAPI_BitBlt($hDC, 0, 0, 800, 200, $hDC_BG, 0, 0, $SRCCOPY)
    _WinAPI_DeleteDC($hDC_BG)
    _WinAPI_DeleteObject($hBMP_BG)

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

    _WinAPI_DrawIconEx($hDC, 0, 0, $hIcon, 800, 200)
    _WinAPI_DestroyIcon($hIcon)
    _WinAPI_ReleaseDC($hGui, $hDC)

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

    While GUIGetMsg() <> -3
    Sleep(10)
    WEnd

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

    Func _CreateBitmap()
    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND(_WinAPI_GetDesktopWindow())
    Local $hBitmap = _GDIPlus_BitmapCreateFromGraphics(800, 200, $hGraphics)
    _GDIPlus_GraphicsDispose($hGraphics)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
    Local $hPen = _GDIPlus_PenCreate(0xFF00FF00)

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

    For $i = 1 To 8
    _GDIPlus_BrushSetSolidColor($hBrush, BitOR(BitShift($i * 0xFF / 8, -24), 0x000000FF))
    _GDIPlus_GraphicsFillEllipse($hContext, ($i - 1) * 100, 10, 100, 100, $hBrush)
    Next
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hContext)
    Return $hBitmap
    EndFunc ;==>_CreateBitmap

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

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _WinAPI_CreateIconIndirect
    ; Description....: Creates an icon or cursor that has the specified size, colors, and bit patterns.
    ; Syntax.........: _WinAPI_CreateIconIndirect ( $hBitmap, $hMask [, $XHotspot [, $YHotspot [, $fIcon]]] )
    ; Parameters.....: $hBitmap - Handle to the icon color bitmap.
    ; $hMask - Handle to the icon bitmask bitmap.
    ; $XHotspot - Specifies the x-coordinate of a cursor's hot spot. If creates an icon, the hot spot is always in
    ; the center of the icon, and this member is ignored.
    ; $YHotspot - Specifies the y-coordinate of the cursor's hot spot. If creates an icon, the hot spot is always in
    ; the center of the icon, and this member is ignored.
    ; $fIcon - Specifies whether creates an icon or a cursor, valid values:
    ; |TRUE - Creates an icon. (Default)
    ; |FALSE - Creates a cursor.
    ; Return values..: Success - Handle to the icon or cursor that is created.
    ; Failure - 0 and sets the @error flag to non-zero.
    ; Author.........: Yashied
    ; Modified.......:
    ; Remarks........: The system copies the bitmaps before creating the icon or cursor. Because the system may temporarily
    ; select the bitmaps in a device context, $hBitmap and $hMask should not already be selected into a device context.
    ; The application must continue to manage the original bitmaps and delete them by _WinAPI_DeleteObject() when they
    ; are no longer necessary.
    ; Related........:
    ; Link...........: @@MsdnLink@@ CreateIconIndirect
    ; Example........: Yes
    ; ===============================================================================================================================

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

    Func _WinAPI_CreateIconIndirect($hBitmap, $hMask, $XHotspot = 0, $YHotspot = 0, $fIcon = 1)

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

    Local $tICONINFO = DllStructCreate($tagICONINFO)

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

    DllStructSetData($tICONINFO, 1, $fIcon)
    DllStructSetData($tICONINFO, 2, $XHotspot)
    DllStructSetData($tICONINFO, 3, $YHotspot)
    DllStructSetData($tICONINFO, 4, $hMask)
    DllStructSetData($tICONINFO, 5, $hBitmap)

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

    Local $Ret = DllCall('user32.dll', 'ptr', 'CreateIconIndirect', 'ptr', DllStructGetPtr($tICONINFO))

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

    If (@error) Or (Not $Ret[0]) Then
    Return SetError(1, 0, 0)
    EndIf
    Return $Ret[0]
    EndFunc ;==>_WinAPI_CreateIconIndirect

    [/autoit]

    und das ist das Ergebnis:autoit.de/wcf/attachment/14121/
    Die Alphawerte der einzelnen Kreise bleiben doch erhalten...

    E