Jump to content

GDI+ Save Image from GUI


Recommended Posts

I'm trying to save images of a digital clock as it updates. The code works except I'm confused by extracting an 'image' from my graphic object. All the examples I've found seem to start out with a screen grabbed image or one loaded from disk. I'm sure it's something stupid I'm missing, any guidence would be appriciated.

; Create GUI
$hGUI = GUICreate("GDI+", $Width, $Height)
GUISetState()

; Draw a string
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBrush = _GDIPlus_BrushCreateSolid(0xffffffff)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 24, 2)
$tLayout = _GDIPlus_RectFCreate(0, 0, $Width, $Height)
Global $hImage, $hBitmap

AdlibRegister("_DrawDigitalClock", 500)

; 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_ImageDispose($hImage)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()

Func _DrawDigitalClock()
_GDIPlus_GraphicsClear($hGraphic,0xFF000000)
$Hour = Int(@HOUR)
_GDIPlus_GraphicsDrawStringEx($hGraphic, $Hour & ":" & @MIN & ":" & @Sec, $hFont, $tLayout, $hFormat, $hBrush)

;Works up til now
$hBitmap = _GDIPlus_BitmapCreateFromGraphics(200, 40, $hgraphic)
_GDIPlus_ImageSaveToFile ($hBitmap, "c:\Dig-Clock.png") ; Saves blank image
EndFunc
Link to comment
Share on other sites

Try this:

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
; Create GUI
$Width = 200
$Height = 40
$hGUI = GUICreate("GDI+", $Width, $Height)
GUISetState()

; Draw a string
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($Width, $Height, $hgraphic)
$hCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hCtxt, 2)
DllCall($ghGDIPDll, "uint", "GdipSetTextRenderingHint", "handle", $hCtxt, "int", 4)
$hBrush = _GDIPlus_BrushCreateSolid(0xffffffff)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 24, 2)
$tLayout = _GDIPlus_RectFCreate(0, 0, $Width, $Height)

_DrawDigitalClock()
AdlibRegister("_DrawDigitalClock", 1000)

; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

$sFile = @ScriptDir & "Dig-Clock.png"
_GDIPlus_ImageSaveToFile ($hBitmap, $sFile) ; Saves blank image

; Clean up resources
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hCtxt)
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()
GUIDelete()
ShellExecute($sFile)
Exit

Func _DrawDigitalClock()
    _GDIPlus_GraphicsClear($hCtxt,0xFF000000)
    $Hour = Int(@HOUR)
    _GDIPlus_GraphicsDrawStringEx($hCtxt, $Hour & ":" & @MIN & ":" & @Sec, $hFont, $tLayout, $hFormat, $hBrush)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap, 0, 0)
EndFunc

Br,

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

That basically works! I moved the save command to inside my function so it happens every time the clock updates. This looks crazy but the PNG file is being watched by a video titling system to update an on-screen clock.

Thanks!

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...