Jump to content
Advert

Recommended Posts

Posted (edited)

Hello everybody :)

@UEZ explained in track ticket 4098 why plenty of graphics context disappear in the examples of the help file, he also gave a solution to prevent this. Here are a couple of tests done on some help file examples :

1) Topic _GDIPlus_LineBrushCreate (original code)
The graphics context disappear if you minimize the GUI OR drag the GUI outside the screen bounds OR cover the GUI with another window :

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    _GDIPlus_Startup() ;initialize GDI+
    Local Const $iWidth = 600, $iHeight = 600, $iBgColor = 0x303030 ;$iBgColor format RRGGBB

    Local $hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight) ;create a test GUI
    GUISetBkColor($iBgColor, $hGUI) ;set GUI background color
    GUISetState(@SW_SHOW)

    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle

    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;sets the graphics object rendering quality (antialiasing)
    Local $hBrush = _GDIPlus_LineBrushCreate(0, 0, 280, 500, 0xFFFF0000, 0xFF4020FF, 1) ;create linear gradient brush
    _GDIPlus_GraphicsFillEllipse($hGraphics, 100, 50, 380, 500, $hBrush) ;draw the egg

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>Example

2) Same topic _GDIPlus_LineBrushCreate (reworked code)
7 lines added in 2 groups (3+4 lines)
* 1st group of 3 lines just before original Local $hGraphics = ... (this line to be commented out or deleted)
* 2nd group of 4 lines just before the main loop

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    _GDIPlus_Startup() ;initialize GDI+
    Local Const $iWidth = 600, $iHeight = 600, $iBgColor = 0x303030 ;$iBgColor format RRGGBB

    Local $hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight) ;create a test GUI
    GUISetBkColor($iBgColor, $hGUI) ;set GUI background color
    GUISetState(@SW_SHOW)

    ;============ 1st part to keep graphics context showing (3 lines added) ============
    Local $idPic = GUICtrlCreatePic("", 0, 0, $iWidth, $iHeight)
    Local $hImageScan0 = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImageScan0)
    ;===================================================================================

    ;;;;; Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle ; <=== LINE REMOVED

    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;sets the graphics object rendering quality (antialiasing)
    Local $hBrush = _GDIPlus_LineBrushCreate(0, 0, 280, 500, 0xFFFF0000, 0xFF4020FF, 1) ;create linear gradient brush
    _GDIPlus_GraphicsFillEllipse($hGraphics, 100, 50, 380, 500, $hBrush) ;draw the egg

    ;============ 2nd part to keep graphics context showing (4 lines added) ============
    Local $hBitmap_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageScan0)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI)) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
    _WinAPI_DeleteObject($hBitmap_GDI)
    _GDIPlus_ImageDispose($hImageScan0)
    ;===================================================================================

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>Example

Now the graphics context never disappear !
I immediately tried this on another example of the help file :

3) Topic _GDIPlus_LineBrushSetSigmaBlend (reworked code)
Exactly the same 7 lines added in 2 groups (3+4 lines) and the original Local $hGraphics = ... commented out (or deleted)

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    _GDIPlus_Startup() ;initialize GDI+
    Local Const $iWidth = 600, $iHeight = 300, $iBgColor = 0x303030 ;$iBgColor format RRGGBB

    Local $hGUI = GUICreate("GDI+ Example (" & @ScriptName & ")", $iWidth, $iHeight) ;create a test GUI
    GUISetBkColor($iBgColor, $hGUI) ;set GUI background color
    GUISetState(@SW_SHOW)

    ;============ 1st part to keep graphics context showing (3 lines added) ============
    Local $idPic = GUICtrlCreatePic("", 0, 0, $iWidth, $iHeight)
    Local $hImageScan0 = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImageScan0)
    ;===================================================================================

    ;;;;; Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI) ;create a graphics object from a window handle ; <=== LINE REMOVED

    _GDIPlus_GraphicsSetSmoothingMode($hGraphics, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;sets the graphics object rendering quality (antialiasing)
    Local $hBrush = _GDIPlus_LineBrushCreate(0, 0, 0, 280, 0xFFFFFF00, 0xFF0000FF, 1) ;create line brush
    _GDIPlus_GraphicsFillRect($hGraphics, 10, 10, 280, 280, $hBrush) ;draw the filled rectangle
    _GDIPlus_LineBrushSetSigmaBlend($hBrush, 0.333, .85)
    _GDIPlus_GraphicsFillRect($hGraphics, 310, 10, 280, 280, $hBrush) ;draw the filled rectangle
    _GDIPlus_GraphicsDrawString($hGraphics, "w/o sigma blend", 14, 14)
    _GDIPlus_GraphicsDrawString($hGraphics, "w/ sigma blend", 314, 14)

    ;============ 2nd part to keep graphics context showing (4 lines added) ============
    Local $hBitmap_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageScan0)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI)) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
    _WinAPI_DeleteObject($hBitmap_GDI)
    _GDIPlus_ImageDispose($hImageScan0)
    ;===================================================================================

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ;cleanup GDI+ resources
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
EndFunc   ;==>Example

This should help users who need their graphics context to be always shown.
All credits go to UEZ.

Edited by pixelsearch
typo

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Advert
Posted (edited)

Thanks

what the solution for _GDIPlus_DrawImageFXEx()

Quote

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

_Example()

Func _Example()
    If Not _GDIPlus_Startup() Or @extended < 6 Then
        MsgBox($MB_SYSTEMMODAL, "ERROR", "GDIPlus.dll v1.1 not available")
        Return
    EndIf

    Local $sFile = FileOpenDialog("Select an image", "", "Images (*.bmp;*.png;*.jpg;*.gif;*.tif)")
    If @error Or Not FileExists($sFile) Then Return

    Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)

    Local $iWidth = 600
    Local $iHeight = _GDIPlus_ImageGetHeight($hImage) * 600 / _GDIPlus_ImageGetWidth($hImage)

    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $iWidth, $iHeight)
    _GDIPlus_GraphicsDispose($hContext)

    Local $hGui = GUICreate("GDI+ v1.1 (" & @ScriptName & ")", $iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui)
    GUISetState(@SW_SHOW)

    Local $hEffect = _GDIPlus_EffectCreate($GDIP_HueSaturationLightnessEffectGuid)
    Local $tEffectParameters = DllStructCreate($tagGDIP_EFFECTPARAMS_HueSaturationLightness)

    Local $fW, $fH
    For $i = 1 To 140
        DllStructSetData($tEffectParameters, "HueLevel", Random(-180, 180, 1))
        _GDIPlus_EffectSetParameters($hEffect, $tEffectParameters)
        $fW = Random(20, 100)
        $fH = Random(20, 100)
        _GDIPlus_DrawImageFXEx($hGraphics, $hBitmap, $hEffect, Random(0, $iWidth - $fW), Random(0, $iHeight - $fH), $fW, $fH)
    Next

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_EffectDispose($hEffect)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
EndFunc   ;==>_Example

 

Edited by jpm
Posted (edited)

Sorry if I interrupt but I think I had similar problem in the past (still not solved) with my:

Func _QPDF_Display(ByRef $oQP, $sPDF_Display_Comment = '', $iLeft = -1, $iTop = 0, $iWidth = 600, $iSelectPage = 1)
    __QPDF_IsUnlocked($oQP)
    If @error Then Return SetError(@error, @extended, $QPDF_RET_FAILURE)

    Local $sPDF_Display_Title = 'QuickPDF Test Display: ' & $sPDF_Display_Comment
    ; select Page
    If $iSelectPage = -1 Then $iSelectPage = $oQP.PageCount
    $oQP.SelectPage($iSelectPage) ;
    ; @TODO     _QPDF_SetDefaultCoordinates($oQP)
    _QPDF_SetDefaultCoordinates($oQP)
;~  $oQP.SetOrigin($QPDF_SORIGIN_BottomLeft)
;~  $oQP.SetMeasurementUnits($QPDF_MUNITS_Milimeters)
    $oQP.NormalizePage(0)
    $oQP.CombineContentStreams()
    $oQP.SelectContentStream(1)
    ; Local $sOutput
    ; $sOutput &= BinaryToString($oQP.GetContentStreamToVariant()) & @CRLF
    ; $oQP.SelectContentStream(1)

    ; calculate DPI
    Local $iPageWidth = $oQP.PageWidth()
    Local $iPageHeight = $oQP.PageHeight()
    ; Local $iInches = $iPageWidth / 72
    Local $iDisplayDPI = ($iWidth / $iPageWidth) * 72 / 3

    ; create GUI
    Local $iHeight = ($iWidth * $iPageHeight) / $iPageWidth
    Local $hWND_QP_TestDisplay = GUICreate($sPDF_Display_Title, $iWidth, $iHeight, $iLeft, $iTop)
    Local $hDC = _WinAPI_GetDC($hWND_QP_TestDisplay)
    GUISetState(@SW_SHOW)

    ; render QP object to Device Context
    $oQP.RenderPageToDC($iDisplayDPI, $iSelectPage, $hDC)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd
    _WinAPI_ReleaseDC($hWND_QP_TestDisplay, $hDC)
    GUIDelete($hWND_QP_TestDisplay) ; Deletes a GUI window and all controls that it contains.
EndFunc   ;==>_QPDF_Display

This example just create window, get DC and than render QP object to Device Context.

; create GUI
    Local $iHeight = ($iWidth * $iPageHeight) / $iPageWidth
    Local $hWND_QP_TestDisplay = GUICreate($sPDF_Display_Title, $iWidth, $iHeight, $iLeft, $iTop)
    Local $hDC = _WinAPI_GetDC($hWND_QP_TestDisplay)
    GUISetState(@SW_SHOW)

    ; render QP object to Device Context
    $oQP.RenderPageToDC($iDisplayDPI, $iSelectPage, $hDC)

But maybe this is not the same issue.
But in any case I could open separate topic for this issue and help request.

Edited by mLipok

Signature beginning:
* Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
* ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Code * for other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * 

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors  * HTML editor * 

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Posted
On 6/9/2026 at 4:02 PM, jpm said:

what the solution for _GDIPlus_DrawImageFXEx()

@jpm Salut Jean-Paul
I couldn't answer your question until I accessed a W11 laptop today to test it.
I added exactly the same code as mentioned in my preceding posts and everything worked fine with the example found in help file, function  _GDIPlus_DrawImageFXEx()

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

_Example()

Func _Example()
    If Not _GDIPlus_Startup() Or @extended < 6 Then
        MsgBox($MB_SYSTEMMODAL, "ERROR", "GDIPlus.dll v1.1 not available")
        Return
    EndIf

    Local $sFile = FileOpenDialog("Select an image", "", "Images (*.bmp;*.png;*.jpg;*.gif;*.tif)")
    If @error Or Not FileExists($sFile) Then Return

    Local $hImage = _GDIPlus_ImageLoadFromFile($sFile)

    Local $iWidth = 600
    Local $iHeight = _GDIPlus_ImageGetHeight($hImage) * 600 / _GDIPlus_ImageGetWidth($hImage)

    Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsDrawImageRect($hContext, $hImage, 0, 0, $iWidth, $iHeight)

    Local $hGui = GUICreate("GDI+ v1.1 (" & @ScriptName & ")", $iWidth, $iHeight)

    ;============ 1st part to keep graphics context showing (3 lines added) ============
    Local $idPic = GUICtrlCreatePic("", 0, 0, $iWidth, $iHeight)
    Local $hImageScan0 = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImageScan0)
    ;===================================================================================

    ;;;;; Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui) ; REMOVE THIS LINE <==============

    GUISetState(@SW_SHOW)

    Local $hEffect = _GDIPlus_EffectCreate($GDIP_HueSaturationLightnessEffectGuid)
    Local $tEffectParameters = DllStructCreate($tagGDIP_EFFECTPARAMS_HueSaturationLightness)

    Local $fW, $fH
    For $i = 1 To 140
        DllStructSetData($tEffectParameters, "HueLevel", Random(-180, 180, 1))
        _GDIPlus_EffectSetParameters($hEffect, $tEffectParameters)
        $fW = Random(20, 100)
        $fH = Random(20, 100)
        _GDIPlus_DrawImageFXEx($hGraphics, $hBitmap, $hEffect, Random(0, $iWidth - $fW), Random(0, $iHeight - $fH), $fW, $fH)
    Next

    ;============ 2nd part to keep graphics context showing (4 lines added) ============
    Local $hBitmap_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageScan0)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI)) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
    _WinAPI_DeleteObject($hBitmap_GDI)
    _GDIPlus_ImageDispose($hImageScan0)
    ;===================================================================================

    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    _GDIPlus_GraphicsDispose($hContext)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_EffectDispose($hEffect)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
EndFunc   ;==>_Example

The effects are always visible, no matter the gui is minimized/restored, or dragged a bit offscreen, or covered by another window. Hope it will work for you too.
 

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted (edited)
3 hours ago, jpm said:

Thanks I will use this new way in the _GDIPlus example

Hello Jean-Paul :)
Of course only for examples using a graphics context, but that still should be a lot of rework, as there are many examples. If you don't mind, one thing needs to be discussed concerning variable names :

"My" variable names $hImageScan0 and $hBitmap_GDI aren't found in the help file, that's a good thing !

Variable name $idPic is found only twice in the _GDIPlus_ examples :
1) in "_GDIPlus_BitmapCreateDIBFromBitmap" example, but it doesn't need to be reworked.

2) in "_GDIPlus_BitmapCreateFromFile" exampleS :

* 2nd example is same example as "_GDIPlus_BitmapCreateDIBFromBitmap" listed just before : no need of rework.

* 1st example doesn't contain a variable name $idPic (great) but look at the other variable names in the 1st part to add

;============ 1st part to keep graphics context showing (3 lines added) ============
Local $idPic = GUICtrlCreatePic("", 0, 0, $iWidth, $iHeight)
Local $hImageScan0 = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImageScan0)
;===================================================================================

There are no $iWidth, $iHeight or $hGraphics in the original example.
Instead we find 400, 300 and $hGraphic (without a final "s")

Also, the cleaning of the resources is done before the main loop in the original example (that seems to never happen in nearly all other _GDIPlus examples)

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_BitmapDispose($hBitmap)

; Shut down GDI+ library
_GDIPlus_Shutdown()

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

So adding the 2nd part of 4 lines "just before the main loop" won't work because GDI+ has already been closed :

;============ 2nd part to keep graphics context showing (4 lines added) ============
Local $hBitmap_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageScan0)
_WinAPI_DeleteObject(GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI)) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
_WinAPI_DeleteObject($hBitmap_GDI)
_GDIPlus_ImageDispose($hImageScan0)
;===================================================================================

You'll have to add the 2nd part before the resource cleaning, or (better ?) move the resource cleaning after the main loop, as found in nearly all other _GDIPlus examples . Here is a functional reworked 1st example of _GDIPlus_BitmapCreateFromFile :

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <ScreenCapture.au3>
#include <WinAPIHObj.au3>

Example()

Func Example()
    Local $hGUI, $hBMP, $hBitmap, $hGraphic, $hImage, $iX, $iY, $hClone

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

    ; Initialize GDI+ library
    _GDIPlus_Startup()

    ; Capture 32 bit bitmap
    $hBMP = _ScreenCapture_Capture("")
    $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

    ; Create 24 bit bitmap clone
    $iX = _GDIPlus_ImageGetWidth($hImage)
    $iY = _GDIPlus_ImageGetHeight($hImage)
    $hClone = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iX, $iY, $GDIP_PXF24RGB)

    ; Save bitmap to file
    _GDIPlus_ImageSaveToFile($hClone, @MyDocumentsDir & "\GDIPlus_Image.bmp")

    ; Clean up resources
    _GDIPlus_BitmapDispose($hClone)
    _GDIPlus_BitmapDispose($hImage)
    _WinAPI_DeleteObject($hBMP)

    ; Draw bitmap to GUI
    $hBitmap = _GDIPlus_BitmapCreateFromFile(@MyDocumentsDir & "\GDIPlus_Image.bmp")

    ;============ 1st part to keep graphics context showing (3 lines added) ============
    Local $idPic = GUICtrlCreatePic("", 0, 0, 400, 300)
    Local $hImageScan0 = _GDIPlus_BitmapCreateFromScan0(400, 300)
    Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImageScan0)
    ;==================================================================================

    ; $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI) ; original line to be commented out (or removed ?)

    _GDIPlus_GraphicsDrawImage($hGraphic, $hBitmap, 0, 0)

   ;============ 2nd part to keep graphics context showing (4 lines added) ============
    Local $hBitmap_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageScan0)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI)) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
    _WinAPI_DeleteObject($hBitmap_GDI)
    _GDIPlus_ImageDispose($hImageScan0)
    ;===================================================================================

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

    ; Clean up resources
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_BitmapDispose($hBitmap)

    ; Shut down GDI+ library
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

3) Same kind of change for example _GDIPlus_PenCreate which doesn't use "$iWidth", "$iHeight" or "$hGraphics"

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    Local $hGUI, $hGraphic, $hPen

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

    ; Draw line
    _GDIPlus_Startup()

    ;============ 1st part to keep graphics context showing (3 lines added) ============
    Local $idPic = GUICtrlCreatePic("", 0, 0, 400, 300)
    Local $hImageScan0 = _GDIPlus_BitmapCreateFromScan0(400, 300)
    Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImageScan0)
    ;==================================================================================

    ; $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI) ; original line to be commented out (or removed ?)

    $hPen = _GDIPlus_PenCreate()
    _GDIPlus_GraphicsDrawLine($hGraphic, 10, 150, 390, 150, $hPen)

   ;============ 2nd part to keep graphics context showing (4 lines added) ============
    Local $hBitmap_GDI = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImageScan0)
    _WinAPI_DeleteObject(GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI)) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
    _WinAPI_DeleteObject($hBitmap_GDI)
    _GDIPlus_ImageDispose($hImageScan0)
    ;===================================================================================

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

    ; Clean up resources
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

If not mistaken, graphics being constantly updated within the main loop shouldn't be reworked.
Good luck Jean-Paul !

Edit: a note about the 2nd group of lines to add, please look at one of them :

_WinAPI_DeleteObject(GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI)) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0

In fact it should be splitted into 2 lines :

Local $hPrevImage = GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0
If $hPrevImage Then _WinAPI_DeleteObject($hPrevImage); delete prev image if any (help file)

But as GUICtrlSendMsg($idPic, ...) will be sent only once in all the reworked examples, then $hPrevImage will always be = 0 and _WinAPI_DeleteObject seems superfluous to avoid a memory leak. Which means, in all reworked examples, one shorter line should be enough :

GUICtrlSendMsg($idPic, 0x0172, 0, $hBitmap_GDI) ; $STM_SETIMAGE = 0x0172, $IMAGE_BITMAP = 0

You'll certainly choose the best way to script it :)

Edited by pixelsearch
added notes about _WinAPI_DeleteObject

"I think you are searching a bug where there is no bug... don't listen to bad advice."

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
×
×
  • Create New...