Jump to content

Displaying then saving an Ellipse with GDI+


Go to solution Solved by Andreik,

Recommended Posts

This image thing will be the destruction of me 😕

OK now I have a simple script to display an image in the GUI and then when I try to save that image to a file on exit, it doesn't save.

What am I missing?

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hGUI, $hWnd, $hImage, $hGraphic

    ; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    $hWnd = WinGetHandle("GDI+")
    GUISetState()

    ; Create a blank image
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)

    ; Draw a circle
    Local $CircleDiameter = 150
    Local $CircleX = (400 - $CircleDiameter) / 2
    Local $CircleY = (300 - $CircleDiameter) / 2
    Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF00FF00) ; Light green color

    _GDIPlus_GraphicsFillEllipse ( $hGraphic, $CircleX, $CircleY, $CircleDiameter, $CircleDiameter, $hBrush)

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Save the image to "circle.png"
    _GDIPlus_ImageSaveToFile($hImage, "circle.png")

    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hImage)
    _GDIPlus_Shutdown()

    If FileExists("circle.png") Then ShellExecute("circle.png")

EndFunc   ;==>_Main

 

Edited by NassauSky
Link to comment
Share on other sites

@Andreik

 

So I'm lost. There are so many GDI_Plus functions and return types.  That's where it gets me.
From your response it seems like we need to somehow create a valid image handle to save the file.   This doesn't seem to help.

Local $hImage = _GDIPlus_BitmapCreateFromGraphics ( 400, 300, $hGraphic )
 _GDIPlus_ImageSaveToFile($hImage, "circle.png")

I am probably missing more.  What technique or code might you use to get an image handle from a displayed image correctly so I can save a file.

Link to comment
Share on other sites

  • Solution

Why don't you tell us what are you trying to achieve so we can help you better? Why don't you create a bitmap and display it properly in a picture control? Something like this:

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hGUI, $hWnd, $hImage, $hGraphic, $cPic

    ; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    $cPic = GUICtrlCreatePic('', 0, 0, 400, 300)
    $hWnd = WinGetHandle("GDI+")
    GUISetState()

    ; Create a blank image
    _GDIPlus_Startup()
    $hImage = _GDIPlus_BitmapCreateFromScan0(400, 300)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)

    ; Draw a circle
    Local $CircleDiameter = 150
    Local $CircleX = (400 - $CircleDiameter) / 2
    Local $CircleY = (300 - $CircleDiameter) / 2
    Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF00FF00) ; Light green color

    _GDIPlus_GraphicsFillEllipse ( $hGraphic, $CircleX, $CircleY, $CircleDiameter, $CircleDiameter, $hBrush)
    BitmapToCtrl($hImage, $cPic)

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Save the image to "circle.png"
    _GDIPlus_ImageSaveToFile($hImage, "circle.png")

    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hImage)
    _GDIPlus_Shutdown()

    If FileExists("circle.png") Then ShellExecute("circle.png")

EndFunc   ;==>_Main

Func BitmapToCtrl($hBitmap, $cCtrl)
    Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
    _WinAPI_DeleteObject(GUICtrlSendMsg($cCtrl, 0x0172, 0, $hHBITMAP))
    _WinAPI_DeleteObject($hHBITMAP)
EndFunc

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

@Andreik

I am trying to create a speech bubble with combining a circle and triangle that displays itself on the screen. The user being able to give an angle from which it points the speech bubble pointer(triangle) from which it then saves with a transparent background the displayed speech bubble to file.

It was my assumption that using a Picture control involves loading an existing image file and displaying it in the control. It's more about displaying pre-existing images. I was trying to figure out code to allow dynamic creation of the image and then preview it in the GUI then save it.

Edited by NassauSky
Link to comment
Share on other sites

@Andreik

 

I thought you were speaking about creating an image using GUICtrlCreatePic.   Also I didn't realize that GDIPlus_GraphicsCreateFromHWND ($hWnd) didn't result in the same type of control as your solution

$hImage = _GDIPlus_BitmapCreateFromScan0(400, 300)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)

I am going to need more practice playing with that and the different types of functions that are available.

 

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