Jump to content

How to add text to image


Recommended Posts

I found this example.

But i need to load a picture from a file and and centered text in the middle, and the save it to a new file name.

Please help.

#include <GuiConstantsEx.au3>

#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()

Local $hGUI, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout

; Create GUI

$hGUI = GUICreate("GDI+", 400, 300)

$hWnd = WinGetHandle("GDI+")

GUISetState()

; Draw a string

_GDIPlus_Startup ()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)

$hBrush = _GDIPlus_BrushCreateSolid (0x7F00007F)

$hFormat = _GDIPlus_StringFormatCreate ()

$hFamily = _GDIPlus_FontFamilyCreate ("Arial")

$hFont = _GDIPlus_FontCreate ($hFamily, 16, 1)

$tLayout = _GDIPlus_RectFCreate (140, 110, 100, 20)

_GDIPlus_GraphicsDrawStringEx ($hGraphic, "INTER05", $hFont, $tLayout, $hFormat, $hBrush)

; Loop until user exits

Do

Until GUIGetMsg() = $GUI_EVENT_CLOSE

; Clean up resources

_GDIPlus_FontDispose ($hFont)

_GDIPlus_FontFamilyDispose ($hFamily)

_GDIPlus_StringFormatDispose ($hFormat)

_GDIPlus_BrushDispose ($hBrush)

_GDIPlus_GraphicsDispose ($hGraphic)

_GDIPlus_Shutdown ()

EndFunc ;==>_Main

Link to comment
Share on other sites

Try openening the GDIPlus include if you are gonna go that route. It's in the include folder in the Autoit directory.

Look at these:

_GDIPlus_ImageLoadFromFile

_GDIPlus_ImageSaveToFile

I dont know how to combine images with GDI sorry, with image magic you combine images like this:

$oMagic = ObjCreate("ImageMagickObject.MagickImage.1")  
    $oMagic.Composite(@ScriptDir &".\Temp\TopImage.bmp", ".\Temp\BottomImage.bmp", ".\Temp\composedImage.bmp")
Link to comment
Share on other sites

cant get it to wrtite text over the picture.

Can anyone help med with this script?

#include <GDIPlus.au3>

_Main()

Func _Main()
    Local $hBitmap, $hImage, $sCLSID, $tData, $tParams, $hWnd, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $GUI_EVENT_CLOSE

    ; Initialize GDI+ library
    _GDIPlus_Startup ()

    ; Load image
     $hImage = _GDIPlus_ImageLoadFromFile ("C:\Temp\1.jpg")
     
    ; Get JPEG encoder CLSID
    $sCLSID = _GDIPlus_EncodersGetCLSID ("JPG")

    ; Draw a string
    _GDIPlus_Startup ()
    $hBrush = _GDIPlus_BrushCreateSolid (0xFF00007F)
    $hFormat = _GDIPlus_StringFormatCreate ()
    $hFamily = _GDIPlus_FontFamilyCreate ("Arial")
    $hFont = _GDIPlus_FontCreate ($hFamily, 12, 2)
    $tLayout = _GDIPlus_RectFCreate (140, 110, 100, 20)
    _GDIPlus_GraphicsDrawStringEx ($hImage, "Hello world", $hFont, $hImage, $hFormat, $hBrush)

    ; Save image 
    _GDIPlus_ImageSaveToFileEx ($hImage, "C:\Temp\desktop.jpg", $sCLSID, DllStructGetPtr($tParams))

    ; Shut down GDI+ library
    _GDIPlus_ShutDown ()

EndFunc      ; Loop until user exits
Edited by borup
Link to comment
Share on other sites

Finally.... he he.

But how do i center the text???

The $sString will be replaced with a automatic pull from the system...

#include <GDIPlus.au3>

Global $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, $hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen
Global $sString="SERVERNAME"

; Initialize GDI+ library
_GDIPlus_StartUp()

; Load image and emboss text
$hImage   = _GDIPlus_ImageLoadFromFile('C:\Temp\1.jpg')
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hFamily  = _GDIPlus_FontFamilyCreate("Arial")
$hFont    = _GDIPlus_FontCreate($hFamily, 38, 1)
$hFormat  = _GDIPlus_StringFormatCreate(0x4000)
$hBrush2  = _GDIPlus_BrushCreateSolid(0xff000000)
$hPen     = _GDIPlus_PenCreate(0xC4000000, 1)
$tLayout = _GDIPlus_RectFCreate (150, 250 )
$aInfo    = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
_GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush2)

; Save image
_GDIPlus_ImageSaveToFile($hImage, @MyDocumentsDir & '\AutoItImage2.bmp')

; Free resources
_GDIPlus_PenDispose         ($hPen    )
_GDIPlus_BrushDispose       ($hBrush1 )
_GDIPlus_BrushDispose       ($hBrush2 )
_GDIPlus_StringFormatDispose($hFormat )
_GDIPlus_FontDispose        ($hFont   )
_GDIPlus_FontFamilyDispose  ($hFamily )
_GDIPlus_GraphicsDispose    ($hGraphic)
_GDIPlus_ImageDispose       ($hImage  )
_GDIPlus_ShutDown()

; Show image
Run("MSPaint.exe " & '"' & @MyDocumentsDir & '\AutoItImage2.bmp"')
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...