Jump to content
Sign in to follow this  
MrChiliCheese

GDI+/WinAPi error

Recommended Posts

MrChiliCheese

Hi,

i have a problem with the following code. After about 30 secons i get an error message:

post-38066-1245613196_thumb.jpg

(For this command enough memory is not available.)

I think it is, because the _Shot function is called in a loop and after a while there isn't enough memory. Or is there a mistake in my code calling GDI+?

#include <ScreenCapture.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("MonView", 490, 280)
GUISetState()

$timer = TimerInit()
Do
    If TimerDiff($timer) > 400 Then
        $timer = TimerInit()
        _Shot()
    EndIf
    Sleep (10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()


Func _Shot()
    _GDIPlus_Startup ()

    $sCap = _GDIPlus_BitmapCreateFromHBITMAP(_ScreenCapture_Capture("",1280,0,3200,1080))
    $sCapX = _GDIPlus_ImageGetWidth($sCap)
    $sCapY = _GDIPlus_ImageGetHeight($sCap)
    $sGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)

    _GDIPlus_GraphicsDrawImageRectRect ($sGraphic, $sCap, 0, 0, $sCapX, $sCapY, 5, 5, 0.25*$sCapX, 0.25*$sCapY)
    _GDIPlus_GraphicsDispose ($sGraphic)
    _GDIPlus_ImageDispose ($sCap)
    _GDIPlus_Shutdown ()
EndFunc

Thanks a lot :D

Share this post


Link to post
Share on other sites
monoceres

Hi! In this line:

$sCap = _GDIPlus_BitmapCreateFromHBITMAP(_ScreenCapture_Capture("",1280,0,3200,1080))

.. there is a memory leak. the hbitmap you recieve from _ScreenCapture_Capture() must be cleaned up with _WinAPI_DeleteObject.


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

Share this post


Link to post
Share on other sites
martin

Hi,

i have a problem with the following code. After about 30 secons i get an error message:

post-38066-1245613196_thumb.jpg

(For this command enough memory is not available.)

I think it is, because the _Shot function is called in a loop and after a while there isn't enough memory. Or is there a mistake in my code calling GDI+?

#include <ScreenCapture.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("MonView", 490, 280)
GUISetState()

$timer = TimerInit()
Do
    If TimerDiff($timer) > 400 Then
        $timer = TimerInit()
        _Shot()
    EndIf
    Sleep (10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()


Func _Shot()
    _GDIPlus_Startup ()

    $sCap = _GDIPlus_BitmapCreateFromHBITMAP(_ScreenCapture_Capture("",1280,0,3200,1080))
    $sCapX = _GDIPlus_ImageGetWidth($sCap)
    $sCapY = _GDIPlus_ImageGetHeight($sCap)
    $sGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)

    _GDIPlus_GraphicsDrawImageRectRect ($sGraphic, $sCap, 0, 0, $sCapX, $sCapY, 5, 5, 0.25*$sCapX, 0.25*$sCapY)
    _GDIPlus_GraphicsDispose ($sGraphic)
    _GDIPlus_ImageDispose ($sCap)
    _GDIPlus_Shutdown ()
EndFunc

Thanks a lot :D

You need to read the help for _ScreenCapture_Capture :D

#include <ScreenCapture.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("MonView", 490, 280)
GUISetState()

$timer = TimerInit()
Do
    If TimerDiff($timer) > 400 Then
        $timer = TimerInit()
        _Shot()
    EndIf
    Sleep(10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()


Func _Shot()
    _GDIPlus_Startup()
    $hBm = _ScreenCapture_Capture("", 1280, 0, 3200, 1080)
    $sCap = _GDIPlus_BitmapCreateFromHBITMAP($hBm)
    _WinAPI_DeleteObject($hBm)
    $sCapX = _GDIPlus_ImageGetWidth($sCap)
    $sCapY = _GDIPlus_ImageGetHeight($sCap)
    $sGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

    _GDIPlus_GraphicsDrawImageRectRect($sGraphic, $sCap, 0, 0, $sCapX, $sCapY, 5, 5, 0.25 * $sCapX, 0.25 * $sCapY)
    _GDIPlus_GraphicsDispose($sGraphic)
    _GDIPlus_ImageDispose($sCap)

    _GDIPlus_Shutdown()
EndFunc  ;==>_Shot

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
UEZ

I just want to add that you don't need to initialize GDI+ in every loop:

#include <ScreenCapture.au3>
#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>
#include <GDIplus.au3>

_GDIPlus_Startup()
$hGUI = GUICreate("MonView", 490, 280)
GUISetState()

$timer = TimerInit()
Do
    If TimerDiff($timer) > 400 Then
        $timer = TimerInit()
        _Shot()
    EndIf
    Sleep(10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()
_GDIPlus_Shutdown()

Func _Shot()
    $hBm = _ScreenCapture_Capture("", 1280, 0, 3200, 1080)
    $sCap = _GDIPlus_BitmapCreateFromHBITMAP($hBm)
    _WinAPI_DeleteObject($hBm)
    $sCapX = _GDIPlus_ImageGetWidth($sCap)
    $sCapY = _GDIPlus_ImageGetHeight($sCap)
    $sGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

    _GDIPlus_GraphicsDrawImageRectRect($sGraphic, $sCap, 0, 0, $sCapX, $sCapY, 5, 5, 0.25 * $sCapX, 0.25 * $sCapY)
    _GDIPlus_GraphicsDispose($sGraphic)
    _GDIPlus_ImageDispose($sCap)
EndFunc  ;==>_Shot

UEZ

Edited by 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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
MrChiliCheese

Oh god, that simple... i think i need a RTFM sticker on my monitor :D

thanks @ UEZ :D

Share this post


Link to post
Share on other sites
rover

you also have a GDI objects leak from a minor bug with_ScreenCapture_Capture()

if you don't need the cursor, add False to this line:

$hBm = _ScreenCapture_Capture("", 1280, 0, 3200, 1080, False)[code=auto:0]

or download the attachment _ScreenCapture_Capture() in the Trac ticket

submitted to Trac: _ScreenCapture_Capture(): GDI object leak with cursor capture

#1040


I see fascists...

Share this post


Link to post
Share on other sites
MrChiliCheese

good to know, thanks for the info :)

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  

×