NassauSky Posted October 23, 2023 Share Posted October 23, 2023 (edited) 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? expandcollapse popup#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 October 23, 2023 by NassauSky Link to comment Share on other sites More sharing options...
Andreik Posted October 23, 2023 Share Posted October 23, 2023 $hImage is just declared but it's not a valid image handle. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
NassauSky Posted October 23, 2023 Author Share Posted October 23, 2023 @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 More sharing options...
Solution Andreik Posted October 23, 2023 Solution Share Posted October 23, 2023 (edited) 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: expandcollapse popup#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 October 23, 2023 by Andreik Zedna and NassauSky 2 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
NassauSky Posted October 23, 2023 Author Share Posted October 23, 2023 (edited) @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 October 23, 2023 by NassauSky Link to comment Share on other sites More sharing options...
Andreik Posted October 23, 2023 Share Posted October 23, 2023 As you can see in the example above you don't need a pre-existent image but you can create your image using GDI+ functions and use that image with a picture control. NassauSky 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
NassauSky Posted October 23, 2023 Author Share Posted October 23, 2023 @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 More sharing options...
Andreik Posted October 23, 2023 Share Posted October 23, 2023 The advantage is that if another window overlap your window the image doesn't need to redraw as in your previous example.. NassauSky 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now