Jump to content
Sign in to follow this  
sandin

GDIplus question

Recommended Posts

sandin

This is modified example for create GDI plus graphic, with added option to make imaged of it, but it always turn out black, why?

#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, 12, 2)
    $tLayout = _GDIPlus_RectFCreate (140, 110, 100, 20)
    _GDIPlus_GraphicsDrawStringEx ($hGraphic, "Hello world", $hFont, $tLayout, $hFormat, $hBrush)
    Local $hbitimage = _GDIPlus_BitmapCreateFromGraphics(400, 300, $hGraphic) ;<--- crate image (black?)
    _GDIPlus_ImageSaveToFile($hbitimage, @DesktopDir & "\temp_image.bmp")
    _WinAPI_DeleteObject($hbitimage)
    
    ; 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

Share this post


Link to post
Share on other sites
monoceres

When you use _GDIPlus_BitmapCreateFromGraphics it doesn't mean that the content in the Graphics object are copied to the bitmap, the bitmap only gets the same format as the graphics object.

Easiest way to accomplish what you want is to use a simple double buffering structure.

I have posted a template here: http://www.autoitscript.com/forum/index.ph...mp;#entry617017

And then use _GDIPlus_ImageSaveToFile($bitmap,"blah.png")

:)

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
sandin

thank you :)

#include <GUIConstants.au3>
#include <GDIplus.au3>

HotKeySet("{F11}", "makeSS")

Global Const $width=600
Global Const $height=400
GLobal $title="GDI+"

; Build your GUI here
Opt("GUIOnEventMode",1)
$hwnd=GUICreate($title,$width,$height)
GUISetOnEvent($GUI_EVENT_CLOSE,"close")
GUISetState()

; Load your GDI+ resources here:
_GDIPlus_Startup()
$graphics=_GDIPlus_GraphicsCreateFromHWND($hwnd)
$bitmap=_GDIPlus_BitmapCreateFromGraphics($width,$height,$graphics)
$hBrush = _GDIPlus_BrushCreateSolid (0xFFFFFFFF);(0x2FB3E7FF)
$hFormat = _GDIPlus_StringFormatCreate ()
$hFamily = _GDIPlus_FontFamilyCreate ("Arial")
$hFont = _GDIPlus_FontCreate ($hFamily, 18, 2)
$backbuffer=_GDIPlus_ImageGetGraphicsContext($bitmap)
$txt = "something"
Do
    _GDIPlus_GraphicsClear($backbuffer)
    
    $tLayout = _GDIPlus_RectFCreate (1/$width+50, 1/$height+5, 0, 0)
    _GDIPlus_GraphicsDrawStringEx ($backbuffer, $txt, $hFont, $tLayout, $hFormat, $hBrush)
    
    _GDIPlus_GraphicsDrawImageRect($graphics,$bitmap,0,0,$width,$height)
    Sleep(10)
Until False

Func makeSS()
    _GDIPlus_ImageSaveToFile($bitmap, @DesktopDir & "\blah.png")
    ConsoleWrite("SS made" & @CRLF)
EndFunc

Func close()
    _GDIPlus_GraphicsDispose($backbuffer)
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_Shutdown()
    Exit
EndFunc
Edited by sandin

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  

×