GDI+ Farbe transparent und als neues Bild speichern

  • Hi,

    habe aktuell folgendes kleines Script:

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <GUIConstantsEx.au3>
    Opt("GuiOnEventMode", 1)

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

    OnAutoItExitRegister("_end")

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

    $chipsetpfad = FileOpenDialog("Chipset", @ScriptDir & "\", "Bilder (*.png)", 1)

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

    $newchipset = GUICreate("Neues Chipset", 480, 256)
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
    GUISetOnEvent($GUI_EVENT_PRIMARYUP, "getPixelcolor")
    GUISetState(@SW_SHOW)

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

    _GDIPlus_Startup()

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

    $chipset = _GDIPlus_ImageLoadFromFile($chipsetpfad)
    $graphic = _GDIPlus_GraphicsCreateFromHWND($newchipset)
    _GDIPlus_GraphicsDrawImage($graphic, $chipset, 0, 0)

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

    While 1
    Sleep(100)
    WEnd

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

    Func getPixelcolor()
    $pos = MouseGetPos()
    $pix = PixelGetColor($pos[0], $pos[1])
    $sMsgText = StringFormat("Der hexadezimale Wert der Farbe ist: %#06x\t", $pix)
    MsgBox(0, "PixelGetColor", $sMsgText)
    EndFunc ;==>getPixelcolor

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

    Func CLOSEClicked()
    Exit
    EndFunc ;==>CLOSEClicked

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

    Func _end()
    _GDIPlus_GraphicsDispose($graphic)
    _GDIPlus_ImageDispose($chipset)
    _GDIPlus_Shutdown()
    EndFunc ;==>_end

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

    In der Funktion getPixelcolor() wird der Hexwert der Farbe des Pixel an der aktuellen Mausposition ermittelt. Nun möchte ich diese Farbe im ganzen Bild transparent machen und ale neues PNG speichern. Hoffe es kann mir jemand sagen wie ich das mache.


    Edit: Habs hinbekommen:

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <GUIConstantsEx.au3>
    Opt("GuiOnEventMode", 1)
    Opt("MouseCoordMode", 2)

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

    OnAutoItExitRegister("_end")

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

    $chipsetpfad = FileOpenDialog("Chipset", @ScriptDir & "\", "Bilder (*.png)", 1)

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

    $newchipset = GUICreate("Neues Chipset", 480, 256)
    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
    GUISetOnEvent($GUI_EVENT_SECONDARYUP, "getPixelcolor")
    GUISetState(@SW_SHOW)

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

    _GDIPlus_Startup()

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

    $chipset = _GDIPlus_ImageLoadFromFile($chipsetpfad)
    $graphic = _GDIPlus_GraphicsCreateFromHWND($newchipset)
    _GDIPlus_GraphicsDrawImage($graphic, $chipset, 0, 0)

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

    While 1
    Sleep(100)
    WEnd

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

    Func getPixelcolor()
    $pos = MouseGetPos()
    $pix = _GDIPlus_BitmapGetPixel($chipset, $pos[0], $pos[1])

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

    For $iY = 0 To 256
    For $iX = 0 To 480
    If _GDIPlus_BitmapGetPixel($chipset, $iX, $iY) = $pix Then
    _GDIPlus_BitmapSetPixel($chipset, $iX, $iY, "00000000")
    EndIf
    Next
    Next

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

    MsgBox(0,"","fertig")
    _GDIPlus_GraphicsClear($graphic, 0x00000000)
    _GDIPlus_GraphicsDrawImage($graphic, $chipset, 0, 0)
    _GDIPlus_ImageSaveToFile($chipset, FileSaveDialog("Bild speichern unter", @ScriptDir, "(*.png)", 18) & ".png")
    EndFunc ;==>getPixelcolor

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

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _GDIPlus_BitmapGetPixel
    ; Description ...: Gets the color of a specified pixel in this bitmap
    ; Syntax.........: _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
    ; Parameters ....: $hBitmap - Pointer to the Bitmap object
    ; $iX - The X coordinate of the pixel
    ; $iY - The Y coordinate of the pixel
    ; Return values .: Success - Returns the pixel color of the bitmap
    ; Failure - 0 and either:
    ; |@error and @extended are set if DllCall failed
    ; |$GDIP_STATUS contains a non zero value specifying the error code
    ; Remarks .......: None
    ; Related .......: _GDIPlus_BitmapSetPixel
    ; Link ..........; @@MsdnLink@@ GdipBitmapGetPixel
    ; Example .......; Yes
    ; ===============================================================================================================================
    Func _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipBitmapGetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "uint*", 0)

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

    If @error Then Return SetError(@error, @extended, 0)
    $GDIP_STATUS = $aResult[0]
    Return $aResult[4]
    EndFunc ;==>_GDIPlus_BitmapGetPixel

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

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _GDIPlus_BitmapSetPixel
    ; Description ...: Sets the color of a specified pixel in this bitmap
    ; Syntax.........: _GDIPlus_BitmapSetPixel($hBitmap, $iX, $iY, $iARGB)
    ; Parameters ....: $hBitmap - Pointer to the Bitmap object
    ; $iX - The X coordinate of the pixel
    ; $iY - The Y coordinate of the pixel
    ; $iARGB - The new color of the pixel
    ; Return values .: Success - True
    ; Failure - False and either:
    ; |@error and @extended are set if DllCall failed
    ; |$GDIP_STATUS contains a non zero value specifying the error code
    ; Remarks .......: None
    ; Related .......: _GDIPlus_BitmapGetPixel
    ; Link ..........; @@MsdnLink@@ GdipBitmapSetPixel
    ; Example .......; Yes
    ; ===============================================================================================================================
    Func _GDIPlus_BitmapSetPixel($hBitmap, $iX, $iY, $iARGB)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipBitmapSetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, "uint", $iARGB)

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

    If @error Then Return SetError(@error, @extended, False)
    $GDIP_STATUS = $aResult[0]
    Return $aResult[0] = 0
    EndFunc ;==>_GDIPlus_BitmapSetPixel

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

    Func CLOSEClicked()
    Exit
    EndFunc ;==>CLOSEClicked

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

    Func _end()
    _GDIPlus_GraphicsDispose($graphic)
    _GDIPlus_ImageDispose($chipset)
    _GDIPlus_Shutdown()
    EndFunc ;==>_end

    [/autoit]

    Andy hat mir ein Schnitzel gebacken aber da war ein Raupi drauf und bevor Oscar das Bugfixen konnte kam Alina und gab mir ein AspirinJunkie.

    Einmal editiert, zuletzt von chip (29. Februar 2012 um 14:26)

  • Hi

    Du kannst in GDI+ einen transparenten Farbbereich angeben.
    In diesem Beispiel alle Farben von 0xFFAAAAAA bis 0xFFFFFFFF:

    Spoiler anzeigen
    [autoit]

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

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

    Opt("MustDeclareVars", 1)

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

    _GDIPlus_Startup()

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

    Global $hBMP = _ScreenCapture_Capture()
    Global $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
    _WinAPI_DeleteObject($hBMP)

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

    Global $iWidth = _GDIPlus_ImageGetWidth($hBitmap) * 96 / _GDIPlus_ImageGetHorizontalResolution($hBitmap)
    Global $iHeight = _GDIPlus_ImageGetHeight($hBitmap) * 96 / _GDIPlus_ImageGetVerticalResolution($hBitmap)

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

    Global $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0)
    Global $hBmp_Trans = $aResult[6]
    Global $hGfx_Trans = _GDIPlus_ImageGetGraphicsContext($hBmp_Trans)

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

    $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateImageAttributes", "int*", 0)
    Global $hAttributes = $aResult[1]
    DllCall($ghGDIPDll, "uint", "GdipSetImageAttributesColorKeys", "hwnd", $hAttributes, "int", 0, "int", True, "uint", 0xFFAAAAAA, "uint", 0xFFFFFFFF)
    DllCall($ghGDIPDll, "int", "GdipDrawImageRectRect", "hwnd", $hGfx_Trans, "hwnd", $hBitmap, "float", 0, "float", 0, "float", $iWidth, "float", $iHeight, "float", 0, "float", 0, "float", $iWidth, "float", $iHeight, "int", 2, "hwnd", $hAttributes, "int", 0, "int", 0)

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

    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hGfx_Trans)
    _GDIPlus_ImageSaveToFile($hBmp_Trans, @ScriptDir & "\Test.png")
    _GDIPlus_BitmapDispose($hBmp_Trans)
    _GDIPlus_Shutdown()

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

    ShellExecute(@ScriptDir & "\Test.png")

    [/autoit]

    E

  • Bis ich den Code fertig bekommen habe, hast du es bereits selbst hinbekommen!

    Spoiler anzeigen
    [autoit]


    #include <GDIPlus.au3>
    #include <GUIConstantsEx.au3>
    Opt("GuiOnEventMode", 1)

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

    OnAutoItExitRegister("_end")

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

    $chipsetpfad = FileOpenDialog("Chipset", @ScriptDir & "\", "Bilder (*.png)", 1)
    _GDIPlus_Startup()

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

    $chipset = _GDIPlus_ImageLoadFromFile($chipsetpfad)
    $iW = _GDIPlus_ImageGetWidth($chipset)
    $iH = _GDIPlus_ImageGetHeight($chipset)
    $newchipset = GUICreate("Neues Chipset", $iW, $iH)
    GUISetState(@SW_SHOW)

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

    $hGraphics = _GDIPlus_GraphicsCreateFromHWND($newchipset)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphics)
    $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImage($hGraphics, $chipset, 0, 0)
    _GDIPlus_GraphicsDrawImage($hContext, $chipset, 0, 0)

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
    GUISetOnEvent($GUI_EVENT_PRIMARYUP, "getPixelcolor")

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

    While 1
    Sleep(100)
    WEnd

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

    Func getPixelcolor()
    $pos = MouseGetPos()
    $pix = PixelGetColor($pos[0], $pos[1])
    $sMsgText = StringFormat("Der hexadezimale Wert der Farbe ist: %#06x\t", $pix)
    MsgBox(0, "PixelGetColor", $sMsgText)
    Local $x, $y, $aResult, $iColor
    For $y = 0 To $iH -1
    For $x = 0 To $iW - 1
    $aResult = DllCall($ghGDIPDll, "uint", "GdipBitmapGetPixel", "hwnd", $hBitmap, "int", $x, "int", $y, "uint*", 0)
    $iColor = $aResult[4]
    If (("0x" & Hex($iColor, 6)) / $pix) = 1 Then DllCall($ghGDIPDll, "uint", "GdipBitmapSetPixel", "hwnd", $hBitmap, "int", $x, "int", $y, "uint", "0x00" & Hex($pix, 6))
    Next
    Next
    _GDIPlus_ImageSaveToFile($hBitmap, @ScriptDir & "\Test.png")
    _end()
    EndFunc ;==>getPixelcolor

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

    Func CLOSEClicked()
    _end()
    EndFunc ;==>CLOSEClicked

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

    Func _end()
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_ImageDispose($chipset)
    _GDIPlus_Shutdown()
    ShellExecute(@ScriptDir & "\Test.png")
    Exit
    EndFunc ;==>_end

    [/autoit]

    Die Ideen waren die Gleichen!

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯