Sign in to follow this  
Followers 0
borup

How to CENTER text on Picture

4 posts in this topic

Please help.

How du i Center the text on the picture????

The text will change from time to time.

#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"')

Share this post


Link to post
Share on other sites



@Borup

It's not perfect, but probably close enough.

If not, I'm sure that from across the gulf of the forums, "intellects vast and cool and unsympathetic"

will drop by to correct this example or post better code.

; tested in beta v3.2.11.1
#include <GDIPlus.au3>
Opt("MustDeclareVars", 1)

Global $hBitmap, $hImage, $hGraphic, $hFamily, $hFont, $tLayout, _
$hFormat, $aInfo, $hBrush1, $hBrush2, $iWidth, $iHeight, $hPen
Global $sString = "SERVERNAME"
Global $fontsize = 38, $font = "Arial", $weight = 800

Local $aData_Len = _GetTextLen($sString, $fontsize, $weight, $font)
;$aData_Len[0] = text length in pixels
;$aData_Len[1] = font height in pixels

; Initialize GDI+ library
_GDIPlus_Startup()

; Load image and emboss text
$hImage   = _GDIPlus_ImageLoadFromFile('C:\Temp\1.jpg')
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
$hFamily = _GDIPlus_FontFamilyCreate($font)
$hFont = _GDIPlus_FontCreate($hFamily, $fontsize, 1, 3)

; subtract a kludge value (20 - play around with value) due to difference in returned text length
; because of difference in font kerning/boldness between font in _GetTextLen() label and font in graphic
$iWidth = ((_GDIPlus_ImageGetWidth($hImage) - $aData_Len[0]) - 20) / 2
$iHeight = (_GDIPlus_ImageGetHeight($hImage) - $aData_Len[1]) / 2

$hFormat = _GDIPlus_StringFormatCreate(0x4000)
$hBrush2 = _GDIPlus_BrushCreateSolid(0xff000000)
$hPen = _GDIPlus_PenCreate(0xC4000000, 1)
$tLayout = _GDIPlus_RectFCreate($iWidth, $iHeight)
$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"')

Func _GetTextLen($s_Data, $i_FontSize = 8.5, $i_FontWeight = -1, $s_TextFont = "Arial")
    ; posted by 'Details', cleanup by 'MsCreator'
    ; from this thread
    ; Calculate required size for gui item
    ; http://www.autoitscript.com/forum/index.php?showtopic=62402
    ; and
    ; get the size of a string in pixels?
    ; http://www.autoitscript.com/forum/index.php?showtopic=13733
    Local Const $DEFAULT_CHARSET = 0 ; ANSI character set
    Local Const $OUT_CHARACTER_PRECIS = 2
    Local Const $CLIP_DEFAULT_PRECIS = 0
    Local Const $PROOF_QUALITY = 2
    Local Const $FIXED_PITCH = 1
    Local Const $RGN_XOR = 3
    Local Const $LOGPIXELSY = 90

    If $i_FontWeight = "" Or $i_FontWeight = -1 Then $i_FontWeight = 400 ; default Font weight
    Local $h_GDW_GUI = GUICreate("Get Data Width", 10, 10, -100, -100, 0x80880000, 0x00000080)

    Local $ah_DC = DllCall("user32.dll", "int", "GetDC", "hwnd", $h_GDW_GUI)
    If @error Then Return SetError(@error, @extended, "")
    Local $intDeviceCap = DllCall("gdi32.dll", "long", "GetDeviceCaps", "int", $ah_DC[0], "long", $LOGPIXELSY)
    If @error Then Return SetError(@error, @extended, "")
    Local $intFontHeight = DllCall("kernel32.dll", "long", "MulDiv", "long", $i_FontSize, "long", $intDeviceCap[0], "long", 72)
    If @error Then Return SetError(@error, @extended, "")
    Local $ah_Font = DllCall("gdi32.dll", "hwnd", "CreateFont", "int", -$intFontHeight[0], _
            "int", 0, "int", 0, "int", 0, "int", $i_FontWeight, "int", 0, _
            "int", 0, "int", 0, "int", $DEFAULT_CHARSET, _
            "int", $OUT_CHARACTER_PRECIS, "int", $CLIP_DEFAULT_PRECIS, _
            "int", $PROOF_QUALITY, "int", $FIXED_PITCH, "str", $s_TextFont)
    If @error Then Return SetError(@error, @extended, "")
    DllCall("gdi32.dll", "hwnd", "SelectObject", "int", $ah_DC[0], "hwnd", $ah_Font[0])
    If @error Then Return SetError(@error, @extended, "")
    Local $stRet = DllStructCreate("int;int")
    DllCall("gdi32.dll", "int", "GetTextExtentPoint32", _
            "int", $ah_DC[0], "str", $s_Data, "long", StringLen($s_Data), "ptr", DllStructGetPtr($stRet))
    If @error Then Return SetError(@error, @extended, "")
    Local $a_RetLen[2] = [DllStructGetData($stRet, 1), DllStructGetData($stRet, 2)]
    GUIDelete($h_GDW_GUI)

    Return $a_RetLen
EndFunc   ;==>_GetTextLen

I see fascists...

Share this post


Link to post
Share on other sites

Thx Rover.

I got it to work.

I played with some of the value and it did it for me.

Perfect :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Is there any method simpler/lighter than this?

EDIT: Got it working.

Local $h_btnimg=GUICtrlCreatePic("images\ButtonImage.gif",$x,$y,$width_nicebtnimg,$height_nicebtnimg)
Local $h_btncaption = GUICtrlCreateLabel($caption, $x, $y, $width_nicebtnimg,$height_nicebtnimg,BitOR($SS_CENTER,$BS_CENTER))
Edited by Lope

Share this post


Link to post
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
Sign in to follow this  
Followers 0