Jump to content

Problem with _GDIPlus_ImageLoadFromFile and deleting file


ESPUser
 Share

Recommended Posts

Hi,

i tried this example from the helpfile.

It creates and then loads an image @MyDocumentsDir & "\GDIPlus_Image.jpg" and i wanted to delete the image when the script ends.

It seems impossible to delete the image when it has been used with _GDIPlus_ImageLoadFromFile even after   _GDIPlus_ImageDispose($hImage) and  _GDIPlus_Shutdown().

Can anyone tell me what i am doing wrong? Is there a way around this?

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

Example()

Func Example()
    Local $hImage, $sCLSID, $tData, $tParams
    
    ; Screen Capture
    _ScreenCapture_Capture(@MyDocumentsDir & "\GDIPlus_Image.jpg")

    ; Initialize GDI+ library
    _GDIPlus_Startup()

    ; Load image
    $hImage = _GDIPlus_ImageLoadFromFile(@MyDocumentsDir & "\GDIPlus_Image.jpg")

    ; Get JPEG encoder CLSID
    $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")

    ; Set up parameters for 90 degree rotation
    $tData = DllStructCreate("int Data")
    DllStructSetData($tData, "Data", $GDIP_EVTTRANSFORMROTATE90)
    $tParams = _GDIPlus_ParamInit(1)
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGTRANSFORMATION, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "Data"))

    ; Save image with rotation
    _GDIPlus_ImageSaveToFileEx($hImage, @MyDocumentsDir & "\GDIPlus_Image2.jpg", $sCLSID, $tParams)

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)

    ; Shut down GDI+ library
    _GDIPlus_Shutdown()

    FileDelete(@MyDocumentsDir & "\GDIPlus_Image.jpg")

    ;ShellExecute(@MyDocumentsDir & "\GDIPlus_Image2.jpg")
EndFunc   ;==>Example

 

Edited by ESPUser
Link to comment
Share on other sites

I'm not an expert with GDI, but i have played around, a bit, earlier.

So far, i have discovered that, when you capture/load an image, with a handle, it gets locked (or it gets maked as being in use , by windows ? ).

It is only when you free the handle that you can delete it.

Therefore you can delete the file after using the

_GDIPlus_ImageDispose($hImage)

command.

Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

After a bit of testing, the file(s) do get deleted only after  _GDIPlus_ImageSaveToFileEx($hImage, @MyDocumentsDir & "\GDIPlus_Image2.jpg", $sCLSID, $tParams) is used , but not before;

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

Example()

Func Example()
    Local $hImage, $sCLSID, $tData, $tParams

    ; Capture screen
    _ScreenCapture_Capture(@MyDocumentsDir & "\GDIPlus_Image.jpg")

    ; Initialize GDI+ library
    _GDIPlus_Startup()

    ; Load image
    $hImage = _GDIPlus_ImageLoadFromFile(@MyDocumentsDir & "\GDIPlus_Image.jpg")
    ; Get JPEG encoder CLSID
    $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")

    ; Set up parameters for 90 degree rotation
    $tData = DllStructCreate("int Data")
    DllStructSetData($tData, "Data", $GDIP_EVTTRANSFORMROTATE90)
    $tParams = _GDIPlus_ParamInit(1)
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGTRANSFORMATION, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "Data"))

    ; Save image with rotation
    _GDIPlus_ImageSaveToFileEx($hImage, @MyDocumentsDir & "\GDIPlus_Image2.jpg", $sCLSID, $tParams)
    FileDelete(@MyDocumentsDir & "\GDIPlus_Image.jpg")
    FileDelete(@MyDocumentsDir & "\GDIPlus_Image2.jpg")
    ; Shut down GDI+ library
    _GDIPlus_Shutdown()

    ;ShellExecute(@MyDocumentsDir & "\GDIPlus_Image2.jpg")
EndFunc   ;==>Example
Edited by Dan_555

Some of my script sourcecode

Link to comment
Share on other sites

This works, no idea what i did wrong before...thanks for your help

 

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

Example()

Func Example()
    Local $hImage, $sCLSID, $tData, $tParams

    ; Screen Capture
    _ScreenCapture_Capture(@MyDocumentsDir & "\GDIPlus_Image.jpg")

    ; Initialize GDI+ library
    _GDIPlus_Startup()

    ; Load image
    $hImage = _GDIPlus_ImageLoadFromFile(@MyDocumentsDir & "\GDIPlus_Image.jpg")

    ; Get JPEG encoder CLSID
    $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")

    ; Set up parameters for 90 degree rotation
    $tData = DllStructCreate("int Data")
    DllStructSetData($tData, "Data", $GDIP_EVTTRANSFORMROTATE90)
    $tParams = _GDIPlus_ParamInit(1)
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGTRANSFORMATION, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "Data"))

    ; Save image with rotation
    _GDIPlus_ImageSaveToFileEx($hImage, @MyDocumentsDir & "\GDIPlus_Image2.jpg", $sCLSID, $tParams)

    ; Clean up resources
    _GDIPlus_ImageDispose($hImage)

    ; Shut down GDI+ library
    _GDIPlus_Shutdown()

    FileDelete(@MyDocumentsDir & "\GDIPlus_Image.jpg")
    FileDelete(@MyDocumentsDir & "\GDIPlus_Image2.jpg")


    ;ShellExecute(@MyDocumentsDir & "\GDIPlus_Image2.jpg")
EndFunc   ;==>Example

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...