Jump to content
Sign in to follow this  
Praxxley

gdiplus is eating all my RAM

Recommended Posts

Praxxley

Hi everyone :)
I'm trying to put two pictures together (first picture on top, second on bottom) and then cut it verticaly and save them..
My script works so far but it eats all of my RAM. I dispose all graphics, bitmaps and images and i tried different orders but i can't find the problem :( Here is my code ($dir1 is the path to the first image, $dir2 to the second and $dirdest is only a part of a path for examle "C:testdir3" and 3 will be used to save 3_1.jpg and 3_2.jpg) :
 

Func _glueImage($dir1,$dir2,$dirdest)
    $hwnd = GUICreate("Clone", 1, 1)
    $graphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
    $imagefromfile=_GDIPlus_ImageLoadFromFile($dir1)
    Local $iW1=_GDIPlus_ImageGetWidth($imagefromfile)
    Local $iH1=_GDIPlus_ImageGetHeight($imagefromfile)
    $imagefromfile2=_GDIPlus_ImageLoadFromFile($dir2)
    Local $iW2=_GDIPlus_ImageGetWidth($imagefromfile2)
    Local $iH2=_GDIPlus_ImageGetHeight($imagefromfile2)
    If($iW1>$iW2) Then
        $bitmap = _GDIPlus_BitmapCreateFromGraphics($iW1, $iH1+$iH2, $graphics)
    Else
        $bitmap = _GDIPlus_BitmapCreateFromGraphics($iW2, $iH1+$iH2, $graphics)
    EndIf
    $backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
    _GDIPlus_GraphicsDrawImageRect($backbuffer, $imagefromfile, 0, 0,$iW1,$iH1)
    _GDIPlus_GraphicsDrawImageRect($backbuffer, $imagefromfile2, 0, $iH1,$iW2,$iH2)
    $graphics2=_GDIPlus_GraphicsCreateFromHWND($hwnd)
    $bitmap2 = _GDIPlus_BitmapCreateFromGraphics($iW1/2, $iH1+$iH2, $graphics2)
    $bitmap2 = _GDIPlus_BitmapCloneArea($bitmap,0,0,$iW1/2,$iH1+$iH2)
    _GDIPlus_ImageSaveToFile($bitmap2,$dirdest&"_1.jpg")
    $bitmap2 = _GDIPlus_BitmapCloneArea($bitmap,$iW1/2,0,$iW2/2,$iH1+$iH2)
    _GDIPlus_ImageSaveToFile($bitmap2,$dirdest&"_2.jpg")

    Local $mem = MemGetStats()
    ConsoleWrite(($mem[1]-$mem[2])/1000&"MB ram in use"&@CRLF)

    _GDIPlus_ImageDispose($imagefromfile)
    _GDIPlus_ImageDispose($imagefromfile2)
    
    Local $mem = MemGetStats()
    ConsoleWrite(($mem[1]-$mem[2])/1000&"MB ram in use"&@CRLF)

    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_BitmapDispose($bitmap2)

    Local $mem = MemGetStats()
    ConsoleWrite(($mem[1]-$mem[2])/1000&"MB ram in use"&@CRLF)

    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_GraphicsDispose($graphics2)

    Local $mem = MemGetStats()
    ConsoleWrite(($mem[1]-$mem[2])/1000&"MB ram in use"&@CRLF)

    _GDIPlus_GraphicsDispose($backbuffer)

    Local $mem = MemGetStats()
    ConsoleWrite(($mem[1]-$mem[2])/1000&"MB ram in use"&@CRLF)

    GUIDelete($hwnd)

EndFunc

It seems like $graphics and $graphics2 do not free their ram..(watch console) Help me please what did i do wrong?

Share this post


Link to post
Share on other sites
UEZ

You are creating several bitmaps/image using one variable. That's not a good idea.

Example:

$bitmap2 = _GDIPlus_BitmapCreateFromGraphics($iW1/2, $iH1+$iH2, $graphics2)
    $bitmap2 = _GDIPlus_BitmapCloneArea($bitmap,0,0,$iW1/2,$iH1+$iH2)
    _GDIPlus_ImageSaveToFile($bitmap2,$dirdest&"_1.jpg")
    $bitmap2 = _GDIPlus_BitmapCloneArea($bitmap,$iW1/2,0,$iW2/2,$iH1+$iH2)

Use one variable for one bitmap/image handle and dispose it afterwards or dispose it before using the variable again.

Br,

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
Praxxley

Oops i didnt know that _GDIPlus_BitmapCloneArea creates a new bitmap.

Thank you for your help i will try it as soon as i can. :)

Share this post


Link to post
Share on other sites
DatMCEyeBall

Help file:

Remarks

When you are done with the Bitmap object, call _GDIPlus_BitmapDispose to release the resources
Have a look at each function in the help file and if you should dispose it then add it to your code.
Edited by DatMCEyeBall

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Share this post


Link to post
Share on other sites
Praxxley

Finaly had time to test it. It now works great :)
Thanks for your support! :)

The solution (in case someone else has the same problem) :

$bitmap2 = _GDIPlus_BitmapCloneArea($bitmap,0,0,$iW1/2,$iH1+$iH2)
_GDIPlus_ImageSaveToFile($bitmap2,$dirdest&"_1.jpg")
_GDIPlus_BitmapDispose($bitmap2)
$bitmap2 = _GDIPlus_BitmapCloneArea($bitmap,$iW1/2,0,$iW2/2,$iH1+$iH2)
_GDIPlus_ImageSaveToFile($bitmap2,$dirdest&"_2.jpg")
_GDIPlus_BitmapDispose($bitmap2)

Always dispose the bitmap you want to save to. Otherwise _GDIPlus_BitmapCloneArea will overwrite the variable and the bitmap remains in ram.

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  

×