Suche eine GDi+ Profi, der mit erklären kann wie man ein Bitmap beschriftet.

    • Offizieller Beitrag

    Servus.

    Ich haben vor ein Bitmap mit einer Beschriftung zu versehen. Die GDiPlus Funktionen hab ich versucht zu berstehen,
    aber für mich klingt das alles wie Bahnhof. Kann mir jemand eine Anleitung geben wie ich dabei vorgehen muß?
    Ich würde mich auch mit eienm Beispiel zufriedengeben, das ich dann auseinanderflücken kann. :D

    Hier mal eine Beispieldatei:
    autoit.de/wcf/attachment/11418/


    Nach dem Beschriften soll das dann ungefär so aussehen:
    autoit.de/wcf/attachment/11417/

    Für eine Hilfe wäre ich dankbar.

  • Das als UDF wäre gut ;D

    Für mein nächstes Projekt wäre das klasse ;D

    Meine Projekte :

    Taschenrechner [X]
    JamLegend Auto-Player [Canceld]
    Launcher [X]
    Multi-Game-Quest-Viewer [Canceld]


    [autoit]

    If $goffy or not $brain Then $DeleteInetCable

    [/autoit]
  • Bin zwar kein Profi, aber so müsste es gehen:

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    $hGUI = GUICreate("_GUICtrlCreate_TextOnControl Test", 181, 78, 192, 124)
    $bPic = GUICtrlCreatePic("test.jpg", 24, 24, 110, 25)
    GUISetState()
    _GUICtrlCreate_TextOnControl($hGUI, $bPic, "TEST", 30, 3)
    While true
    Switch GuiGetMsg()
    Case -3
    Exit

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

    EndSwitch
    WEnd
    Func _GUICtrlCreate_TextOnControl($hWnd, $Ctrl, $sString, $pX, $pY, $sFont="Arial", $sSize=12, $sFormat=0)
    If Not WinExists($hWnd) Then Return SetError(1, "", 0)
    $ctrlHandle=ControlGetHandle($hWnd, "", $Ctrl)
    If $ctrlHandle=0 Then Return SetError(2, "", 0)
    _GDIPlus_StartUp()
    $hGraphics=_GDIPlus_GraphicsCreateFromHWND($ctrlHandle)
    _GDIPlus_GraphicsDrawString($hGraphics, $sString, $pX, $pY, $sFont, $sSize, $sFormat)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    EndFunc

    [/autoit]

    Edit: So kann man auch noch die Farbe wählen:

    Spoiler anzeigen
    [autoit]

    Func _GUICtrlCreate_TextOnControl($hWnd, $Ctrl, $sString, $pX, $pY, $sFont="Arial", $sSize=12, $sFormat=0, $sColor=0xFF000000)
    If Not WinExists($hWnd) Then Return SetError(1, "", 0)
    $ctrlHandle=ControlGetHandle($hWnd, "", $Ctrl)
    If $ctrlHandle=0 Then Return SetError(2, "", 0)
    _GDIPlus_StartUp()
    $hGraphics=_GDIPlus_GraphicsCreateFromHWND($ctrlHandle)
    $sBrush=_GDIPlus_BrushCreateSolid($sColor)
    $sFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    $hFont = _GDIPlus_FontCreate($hFamily, $sSize, $sFormat)
    $sLayout = _GDIPlus_RectFCreate($pX, $pY)
    $sInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sString, $hFont, $sLayout, $sFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $sInfo[0], $sFormat, $sBrush)
    _GDIPlus_BrushDispose($sBrush)
    _GDIPlus_StringFormatDispose($sFormat)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    EndFunc

    [/autoit]

    Einmal editiert, zuletzt von Ineluki (4. Oktober 2010 um 18:44)

    • Offizieller Beitrag

    Ineluki.

    Danke für dein Beispiel, es hilft mir aber nicht weiter. Du manipulierst den Hintergund der GUI.
    Wie das funzt, kann man auch im Hilfebeispiel von _GDIPlus_FontFamilyCreate sehen.
    Ich möchte das Bitmap laden, bearbeiten und wieder speichern.

  • Probier es mal bitte so :

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>

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

    _GDIPlus_Startup ()
    $Bitmap = _GDIPlus_BitmapCreateFromFile ($File)
    $Handle = _GDIPlus_ImageGetGraphicsContext ($Bitmap)
    _GDIPlus_GraphicsDrawString ($Handle,"Beschriftung",$x,$y,"Arial",Default,Default)
    _GDIPlus_ImageSaveToFile ($Bitmap,$File2)

    [/autoit]

    leider habe ich gerade keine Zeit zum Testen , du musst natürlich $File,$x,$y,$File2 anpassen ;)

  • Hab meins angepasst:

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    $imagePath="test.jpg"
    $hGUI = GUICreate("_GUICtrlCreate_TextOnControl Test", 181, 78, 192, 124)
    $bPic = GUICtrlCreatePic($imagePath, 24, 24, 110, 25)
    GUISetState()
    _DrawStringOnImage($imagePath, "TEST", 30, 3)
    GUICtrlSetImage($bPic, $imagePath)
    While true
    Switch GuiGetMsg()
    Case -3
    Exit

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

    EndSwitch
    WEnd
    Func _DrawStringOnImage($iPath, $sString, $pX, $pY, $nPath="", $sFont="Arial", $sSize=12, $sFormat=0, $sColor=0xFF000000)
    If Not FileExists($iPath) Then Return SetError(1, "", 0)
    If $nPath="" Then $nPath=$iPath
    _GDIPlus_StartUp()
    $iBitmap=_GDIPlus_BitmapCreateFromFile($iPath)
    $iGraphics=_GDIPlus_ImageGetGraphicsContext($iBitmap)
    $sBrush=_GDIPlus_BrushCreateSolid($sColor)
    $sFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    $hFont = _GDIPlus_FontCreate($hFamily, $sSize, $sFormat)
    $sLayout = _GDIPlus_RectFCreate($pX, $pY)
    $sInfo = _GDIPlus_GraphicsMeasureString($iGraphics, $sString, $hFont, $sLayout, $sFormat)
    _GDIPlus_GraphicsDrawStringEx($iGraphics, $sString, $hFont, $sInfo[0], $sFormat, $sBrush)
    _GDIPlus_ImageSaveToFile($iBitmap, $nPath)
    _GDIPlus_BrushDispose($sBrush)
    _GDIPlus_StringFormatDispose($sFormat)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_BitmapDispose($iBitmap)
    _GDIPlus_GraphicsDispose($iGraphics)
    _GDIPlus_Shutdown()
    EndFunc

    [/autoit]
    • Offizieller Beitrag

    @nof@ker und Ineluki.

    Beide Beispiel funzen nicht.

  • Ich nehme mal an, dass das Bild nicht gespeichert werden kann, weil das alte noch existiert und nicht gelöscht werden kann, da es noch von GDI+ genutzt wird. Eine bessere Methode als eine temporäre Datei anzulegen fällt mir da leider nicht ein:

    Spoiler anzeigen
    [autoit]

    Func _DrawStringOnImage($iPath, $sString, $pX, $pY, $nPath="", $sFont="Arial", $sSize=12, $sFormat=0, $sColor=0xFF000000)
    If Not FileExists($iPath) Then Return SetError(1, "", 0)
    If $nPath="" Then $nPath=$iPath
    $filename=StringSplit($iPath, "\")
    FileCopy($iPath, @TempDir&"\"&$filename[$filename[0]])
    FileDelete($nPath)
    _GDIPlus_StartUp()
    $iBitmap=_GDIPlus_BitmapCreateFromFile(@TempDir&"\"&$filename[$filename[0]])
    $iGraphics=_GDIPlus_ImageGetGraphicsContext($iBitmap)
    $sBrush=_GDIPlus_BrushCreateSolid($sColor)
    $sFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate($sFont)
    $hFont = _GDIPlus_FontCreate($hFamily, $sSize, $sFormat)
    $sLayout = _GDIPlus_RectFCreate($pX, $pY)
    $sInfo = _GDIPlus_GraphicsMeasureString($iGraphics, $sString, $hFont, $sLayout, $sFormat)
    _GDIPlus_GraphicsDrawStringEx($iGraphics, $sString, $hFont, $sInfo[0], $sFormat, $sBrush)
    _GDIPlus_ImageSaveToFile($iBitmap, $nPath)
    _GDIPlus_BrushDispose($sBrush)
    _GDIPlus_StringFormatDispose($sFormat)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_BitmapDispose($iBitmap)
    _GDIPlus_GraphicsDispose($iGraphics)
    _GDIPlus_Shutdown()
    FileDelete(@TempDir&"\"&$filename[$filename[0]])
    EndFunc

    [/autoit]
  • Meinst du so was?

    Spoiler anzeigen
    [autoit]


    ;Fast hack by UEZ 2010 ;)
    #include <GuiConstantsEx.au3>
    #include <GDIPlus.au3>

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

    Opt('MustDeclareVars', 1)
    Opt("GUIOnEventMode", 1)

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

    _GDIPlus_Startup()
    Local $hBild = _GDIPlus_ImageLoadFromFile("Button1.bmp")
    Local $iX = _GDIPlus_ImageGetWidth($hBild)
    Local $iY = _GDIPlus_ImageGetHeight($hBild)

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

    Local $hGUI = GUICreate("GDI+ Raupi", $iX, $iY)
    GUISetState()

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

    Local $text = "Beispieltext"

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

    Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    Local $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iX, $iY, $hGraphic)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)

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

    Local $hPinsel = _GDIPlus_BrushCreateSolid (0xFFFFFFFF)
    Local $hFormat = _GDIPlus_StringFormatCreate ()
    Local $hFamily = _GDIPlus_FontFamilyCreate ("Arial")
    Local $font_size = Floor(($iX - StringLen($text)) / 10)
    Local $hFont = _GDIPlus_FontCreate ($hFamily, $font_size, 0)
    Local $tLayout = _GDIPlus_RectFCreate (0, 0, 0, 0)
    Local $aInfo = _GDIPlus_GraphicsMeasureString ($hGraphic, $text, $hFont, $tLayout, $hFormat)

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

    _GDIPlus_GraphicsDrawImageRect($hContext, $hBild, 0, 0, $iX, $iY)

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

    Local $fWidth = Floor(DllStructGetData($aInfo[0], "Width"))
    Local $fHeight = Floor(DllStructGetData($aInfo[0], "Height"))

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

    DllStructSetData($tLayout, "x", $iX / 2 - $fWidth / 2)
    DllStructSetData($tLayout, "y", $iY / 2 - $fHeight / 2 - 2)

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

    _GDIPlus_GraphicsDrawStringEx ($hContext, $text, $hFont, $tLayout, $hFormat, $hPinsel)

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

    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iX, $iY)

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

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

    While Sleep(1000)
    WEnd

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

    Func _Exit()
    _GDIPlus_ImageSaveToFile($hBitmap, "Button1+.bmp")
    _GDIPlus_ImageDispose($hBild)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hPinsel)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hContext)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
    Exit
    EndFunc

    [/autoit]

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

    • Offizieller Beitrag

    Ineluki, dein Script funzt soweit.

    UEZ, dein Beispiel ist perfekt. Voralledingen die Fontgröße und Positionierung.

    Hab aber jetzt mal eine Frage dazu. Braucht man die GUI dazu überhaupt?
    Irgendwie kapiere ich nicht für was ein Bitmap von der GUI ertstellt werden muß.
    Kann man das nicht direkt machen?

  • Ineluki, dein Script funzt soweit.

    UEZ, dein Beispiel ist perfekt. Voralledingen die Fontgröße und Positionierung.

    Hab aber jetzt mal eine Frage dazu. Braucht man die GUI dazu überhaupt?
    Irgendwie kapiere ich nicht für was ein Bitmap von der GUI ertstellt werden muß.
    Kann man das nicht direkt machen?


    Kannst ja daraus eine Funktion basteln ;)

    Spoiler anzeigen
    [autoit]


    ;Fast hack by UEZ 2010 ;)
    #include <GDIPlus.au3>
    Opt('MustDeclareVars', 1)
    Opt("GUIOnEventMode", 1)

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

    _GDIPlus_Startup()
    Local $hBild = _GDIPlus_ImageLoadFromFile("Button1.bmp")
    Local $iX = _GDIPlus_ImageGetWidth($hBild)
    Local $iY = _GDIPlus_ImageGetHeight($hBild)

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

    Local $text = "Beispieltext"

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

    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iX, $iY)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hContext, 2)

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

    Local $hPinsel = _GDIPlus_BrushCreateSolid (0xFFFFFFFF)
    Local $hFormat = _GDIPlus_StringFormatCreate ()
    Local $hFamily = _GDIPlus_FontFamilyCreate ("Arial")
    Local $font_size = Floor(($iX - StringLen($text)) / 10)
    Local $hFont = _GDIPlus_FontCreate ($hFamily, $font_size, 0)
    Local $tLayout = _GDIPlus_RectFCreate (0, 0, 0, 0)
    Local $aInfo = _GDIPlus_GraphicsMeasureString ($hContext, $text, $hFont, $tLayout, $hFormat)

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

    _GDIPlus_GraphicsDrawImageRect($hContext, $hBild, 0, 0, $iX, $iY)

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

    Local $fWidth = DllStructGetData($aInfo[0], "Width")
    Local $fHeight = DllStructGetData($aInfo[0], "Height")

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

    DllStructSetData($tLayout, "x", $iX / 2 - Round($fWidth / 2, 0))
    DllStructSetData($tLayout, "y", $iY / 2 - Round($fHeight / 2, 0) - 2)

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

    _GDIPlus_GraphicsDrawStringEx ($hContext, $text, $hFont, $tLayout, $hFormat, $hPinsel)

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

    _Exit()

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

    Func _Exit()
    _GDIPlus_ImageSaveToFile($hBitmap, "Button1+.bmp")
    _GDIPlus_ImageDispose($hBild)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hPinsel)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hContext)
    _GDIPlus_Shutdown()
    Exit
    EndFunc

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

    Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[6]
    EndFunc

    [/autoit]

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

    2 Mal editiert, zuletzt von UEZ (6. Oktober 2010 um 11:53)

    • Offizieller Beitrag

    Danke dir, ich glaub jetzt hab ich das gerafft. :D
    Um GDI+ komplett zu kapieren, muß ich noch gewaltig büffeln.


  • Hab aber jetzt mal eine Frage dazu. Braucht man die GUI dazu überhaupt?
    Irgendwie kapiere ich nicht für was ein Bitmap von der GUI ertstellt werden muß.
    Kann man das nicht direkt machen?

    Grob erklärt gibt es zwei Arten von Bitmaps:
    Device Independent Bitmap (DIB)
    Device Dependent Bitmap (DDB)

    Man muss z.b. die Farbtiefe angeben, wenn man ein Bitmap erstellt.
    Das kann man manuell machen mit z.b: _WinAPI_CreateDibSection wie hier: [ gelöst ] GDI+ unterschiedliche Icon-Transparenz
    Oder man erstellt ein DDB Bitmap mit der Farbtiefe des Bildschirms wie im Beispiel von UEZ - das geht wesentlich schneller und einfacher.

    mfgE