Jump to content

[SOLVED] GDI+ help


Recommended Posts

Hi,

I'm trying to add text to an existing image.

#include <GuiConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#include <array.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local  $hImage, $drawString, $saveFile

    ; Initialize GDI+ library
    _GDIPlus_Startup ()

    ; Load image from File
    $hImage = _GDIPlus_ImageLoadFromFile ("c:\test.jpg")
    ConsoleWrite($hImage & @CRLF)
    
    $drawString = _GDIPlus_GraphicsDrawString ($hImage, "hello", 10, 10)
    ConsoleWrite($drawString & @CRLF)
    
    $saveFile = _GDIPlus_ImageSaveToFile ($hImage, "c:\test2.jpg")
    ConsoleWrite($saveFile & @CRLF)
    
    ; Clean up resources
    _GDIPlus_ImageDispose ($hImage)

    ; Shut down GDI+ library
    _GDIPlus_ShutDown ()

EndFunc   ;==>_Main

drawString is coming back as false, everything else is working as expected. I've tried drawRect, etc and they call come back false.

I've also tried the _GDIPlus_GraphicsDrawStringEx function after setting up font, brush, etc, etc but that also fails.

Am I missing a key concept in using GDI+?

Thanks,

D

Edited by RagsRevenge
Link to comment
Share on other sites

You almost had it just missing a one thing;

#include <GuiConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#include <array.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local  $hImage, $drawString, $saveFile, $hGraphic

    ; Initialize GDI+ library
    _GDIPlus_Startup ()

    ; Load image from File
    $hImage = _GDIPlus_ImageLoadFromFile ("c:\test.jpg")
    ConsoleWrite($hImage & @CRLF)

    $hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage) ;This was missing
    $drawString = _GDIPlus_GraphicsDrawString ($hGraphic, "hello", 10, 10)
    ConsoleWrite($drawString & @CRLF)
    
    $saveFile = _GDIPlus_ImageSaveToFile ($hImage, "c:\test2.jpg")
    ConsoleWrite($saveFile & @CRLF)
    
    ; Clean up resources
    _GDIPlus_ImageDispose ($hImage)

    ; Shut down GDI+ library
    _GDIPlus_ShutDown ()

EndFunc   ;==>_Main

Hope that helps.

Edited by XKahn
Link to comment
Share on other sites

Here one method:

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
Opt('MustDeclareVars', 1)
Opt("GUIOnEventMode", 1)
Local $hWnd1, $hWnd2, $hGraphic, $file, $hImage, $hBackbuffer, $hBitmap
Local $hBrush, $hFamily, $hFormat, $hFont, $tLayout, $aInfo, $sString, $saveFile
Local $iX, $iY

$file = FileOpenDialog("Select image to load", @ScriptDir, "Images (*.jpg;*.png;*.bmp)")

_GDIPlus_Startup()

$hImage = _GDIPlus_ImageLoadFromFile($file)

$iX = _GDIPlus_ImageGetWidth($hImage)
$iY = _GDIPlus_ImageGetHeight($hImage)

$hWnd1 = GUICreate("_GDIPlus_GraphicsDrawImage", $iX, $iY)
GUISetState()

$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd1)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($iX, $iY, $hGraphic)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsDrawImage($hBackbuffer, $hImage, 0, 0)


$hBrush = _GDIPlus_BrushCreateSolid (0xFFF0F0F0)
$hFormat = _GDIPlus_StringFormatCreate ()
$hFamily = _GDIPlus_FontFamilyCreate ("Arial")
$hFont = _GDIPlus_FontCreate ($hFamily, 24, 2)
$tLayout = _GDIPlus_RectFCreate (0, 0, 0, 0)
$sString = "Hello World"
_GDIPlus_GraphicsDrawStringEx ($hBackbuffer, $sString, $hFont, $tLayout, $hFormat, $hBrush)

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

GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

Do
Until Not Sleep(10000000)

Func _Exit()
    $saveFile = _GDIPlus_ImageSaveToFile ($hBitmap, "c:\test2.jpg")
    _GDIPlus_FontDispose ($hFont)
    _GDIPlus_FontFamilyDispose ($hFamily)
    _GDIPlus_StringFormatDispose ($hFormat)
    _GDIPlus_BrushDispose ($hBrush)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hBackbuffer)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    Exit
EndFunc

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

would you not need #Include <GDIPlus.au3> or am I missing something.

It is already included in ScreenCapture.au3 :mellow:

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

It is already included in ScreenCapture.au3 :mellow:

UEZ

Ah. cheers for that.

I probably would have been scratching my head over a duplicate function

if I were to use screencapture().

Lesson for the day, check UDFs #includes before proceeding.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

You almost had it just missing a one thing;

#include <GuiConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WinAPI.au3>
#include <array.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local  $hImage, $drawString, $saveFile, $hGraphic

    ; Initialize GDI+ library
    _GDIPlus_Startup ()

    ; Load image from File
    $hImage = _GDIPlus_ImageLoadFromFile ("c:\test.jpg")
    ConsoleWrite($hImage & @CRLF)

    $hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage) ;This was missing
    $drawString = _GDIPlus_GraphicsDrawString ($hGraphic, "hello", 10, 10)
    ConsoleWrite($drawString & @CRLF)
    
    $saveFile = _GDIPlus_ImageSaveToFile ($hImage, "c:\test2.jpg")
    ConsoleWrite($saveFile & @CRLF)
    
    ; Clean up resources
    _GDIPlus_ImageDispose ($hImage)

    ; Shut down GDI+ library
    _GDIPlus_ShutDown ()

EndFunc   ;==>_Main

Hope that helps.

Thank you XKahn and UEZ. I ended up using _GDIPlus_GraphicsDrawStringEx, but it was that one line that was preventing my code from working.
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...