Jump to content

Cannot load image file


Go to solution Solved by Trong,

Recommended Posts

Hi 

I am having problems loading an image using _GDIPlus_ImageLoadFromFile. Each attempt fails at the "Load error" line of code which shows an error code of 1.  The same error code is returned if the file does not exist yet these  files do exist and I am supplying the full path of the file required within the local filesystem. Also, if I copy and paste the text of the file path from the debug statement in the code into a command prompt and hit enter, it will display the image in question. These are all jpg files created using INetGet. Where might I be going wrong please?  Any help would be appreciated. My code is below.

 

          If FileExists($aImgLst[$j]) Then
              FnuDebug("jpg: " & $aImgLst[$j])
            $hBitmap1 = _GDIPlus_ImageLoadFromFile($aImgLst[$j])
            If @error<>0 then MsgBox(0,"", "Load error " & @error & ", handle=" & $hBitmap1)
            $hBitMap2 = _GDIPlus_ImageScale($hBitMap1, $dScale, $dScale)
            If @error<>0 then MsgBox(0,"", "Scale error " & @error)
            If _GDIPlus_ImageSaveToFile($hBitMap2,$aImgLst[$j] & "Small") <> True Then
              MsgBox(0,"", "Image error: " & @error & " (" & $hBitMap2 & ")")
            EndIf
          EndIf

 

Thank you.

Link to comment
Share on other sites

  • Solution

Take a look at the example script:

#include <GDIPlus.au3>
Global $iImageFile_IN = "C:\Users\Administrator\Desktop\example.jpg"
Global $iImageFile_OUT = "C:\Users\Administrator\Desktop\example_Scale.jpg"
Global $iImage_Encoder = "JPG"
Global $iImage_Scaled = 2.75 ;1.0 is without any scaling

FileDelete($iImageFile_OUT)

If FileExists($iImageFile_IN) Then
    ConsoleWrite("- Image processing... " & $iImageFile_IN & @CRLF)
    _GDIPlus_Startup() ; Initialize GDI+ library
    Local $hImage = _GDIPlus_ImageLoadFromFile($iImageFile_IN)
    If @error <> 0 Then ConsoleWrite("! 1 ImageLoad error " & @error & @CRLF)
    Local $hImage_Scaled = _GDIPlus_ImageScale($hImage, $iImage_Scaled, $iImage_Scaled)
    If @error <> 0 Then ConsoleWrite("! 2 Scale error " & @error & @CRLF)
    ; Get JPEG encoder CLSID
    Local $sCLSID = _GDIPlus_EncodersGetCLSID($iImage_Encoder)
    If @error <> 0 Then ConsoleWrite("! 3 Encoder error " & @error & @CRLF)
    ; Save image
    _GDIPlus_ImageSaveToFileEx($hImage_Scaled, $iImageFile_OUT, $sCLSID)
    If @error <> 0 Then
        ConsoleWrite("! 4 ImageSaveToFile error " & @error & @CRLF)
    Else
        ConsoleWrite("- DONE " & @CRLF)
        ShellExecute($iImageFile_OUT)
    EndIf
    _GDIPlus_Shutdown()    ; Shut down GDI+ library
Else
    MsgBox(64, "", "Image not Exists: " & $iImageFile_IN)
EndIf

 

Regards,
 

Link to comment
Share on other sites

Ok, first of all, thank you once again all and to VIP in particular. I clearly have a lot to learn in this area. I made changes as follows:

  1. Using _GDIPlus_StartUp and _GDIPlus_Shutdown
  2. Using _GDIPlus_ImageSavetoFileEx with encoding instead of _GDIPlus_ImageSavetoFile
  3. Using _GSIPlus_ImageDispose after the scaled image is saved

All now seems to be well and I'll know better next time.

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

×
×
  • Create New...