Jump to content
Sign in to follow this  
matlakala

Memory overflow trying to create lots of images

Recommended Posts

matlakala

Hi Everyone!

Couldn't find a solution in the forum. Now I'm asking...

I'm trying to make a script that reads lines from a text file and creates images out of each line. The text file can contain 50.000 lines and more. For each line an image will be created. Each image contains the content of this line. Dispose sits inside the loop.

First I used _GDIPlus_ImageLoadFromFile and connected functions inside a while loop. After 3000 images, though, the application crashed due to a memory overflow. In another try I had _GDIPlus_ImageLoadFromFile outside the loop but then the second image contained the text lines from the first image and the third from the second and the first and so on. Can someone please help me with this?

[...]
Local $sFileMaterialList = "C:\myFile.txt"
Local $sDestinationDir = @ScriptDir & "\Images"
Local $line = 1

$hMaterialList = FileOpen( $sFileMaterialList, 0 )
DirCreate(sDestinationDir )

;create images and exit if EOF
While(1)
    $sMaterial = FileReadLine ( $hMaterialList , $line )
    If @error = -1 Then
        ExitLoop
    EndIf
    If stringlen($sMaterial)>0 Then
        createImage($sMaterial)
    EndIf
    $line = $line + 1
WEnd
[...]



Func createImage($sMaterial)
    _GDIPlus_Startup ()
    $hImage = _GDIPlus_ImageLoadFromFile ($sBaseImg)
    $hBuffer = _GDIPlus_ImageGetGraphicsContext($hImage)

    _GDIPlus_GraphicsDrawString ($hBuffer, $sMaterial, 20, 20, "Arial", 30 )

    ; Save image to file
    If Not _GDIPlus_ImageSaveToFile ($hImage, $sDestinationDir & "\" & $sMaterial & "." & $sFileExtension) Then
        FileWriteLine(@scriptdir & "\" & $sFileMaterialError, $sMaterial)
        $err = True
    EndIf
    ; Clean up resources & shutdown
    _GDIPlus_ImageDispose ($hImage)
    _GDIPlus_ShutDown ()
EndFunc

Share this post


Link to post
Share on other sites
smashly

Hi,

Dispose of each $hBuffer as well.

Add _GDIPlus_GraphicsDispose($hBuffer) to your clean up.

Cheers

Edited by smashly

Share this post


Link to post
Share on other sites
matlakala

Argh! That was just too simple. :mellow:

Thanks man. Thanks for the rapid response and the solution. It's working fine now.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.