Page 1 of 1

LoadImage()

Posted: Sun May 14, 2006 9:55 pm
by lee__48
Can someone tell me what's wrong here? I am trying see if an image exists, then if so resize it. If not, a new image should be created (not shown here)

I have a lot of images to process so want to use a procedure, however, when a file that doesn't exist is found the program crashes. according to the manual if LoadImage() fails zero should be returned.

Thanks,
Lee

Code: Select all

FileName.s = "(filename here)"
UseJPEGImageDecoder()

; -----------


If LoadImage(0, FileName, #PB_Image_DisplayFormat)
  Debug "1 Loaded"
  ResizeImage(0, 50, 50)
  Debug "1 Resized"
Else
  Debug "1 Not Loaded"
EndIf

; -----------

Procedure OpenImage(ImageID, FileName.s)
 
    a=LoadImage(ImageID, FileName)

  ProcedureReturn a
EndProcedure

If OpenImage(0, FileName)
  Debug "2 Loaded"
  ResizeImage(0, 50, 50)
  Debug "2 Resized"
Else
  Debug "2 Not Loaded"
EndIf

Posted: Sun May 14, 2006 10:01 pm
by srod
..according to the manual if LoadImage() fails zero should be returned.
This probably refers to internal errors (such as insufficient memory or wrong file format etc.) as opposed to external errors such as missing files.

Use FileSize() to determine if the file exists before using LoadImage() etc.

btw, how can you find a file which doesn't exist? :D

Posted: Sun May 14, 2006 10:10 pm
by netmaestro
See if this helps, it's a little thing I mocked up to get you going in the right direction. You just change my d:\wordconnect\ for your own folder with .jpgs in it:

Code: Select all

UseJPEGImageDecoder() 

; ----------- 

  folder.s = "d:\wordconnect\" 
  If ExamineDirectory(0, folder, "*.jpg") 
    While NextDirectoryEntry(0) 
      Debug DirectoryEntryName(0)
      If LoadImage(0, folder+DirectoryEntryName(0), #PB_Image_DisplayFormat) 
        Debug "1 Loaded" 
        ResizeImage(0, 50, 50) 
        Debug "1 Resized" 
      Else 
        Debug "1 Not Loaded" 
      EndIf 
    Wend 
  Else
    Debug "Could not find folder!"
  EndIf

Posted: Sun May 14, 2006 10:31 pm
by lee__48
netmaestro wrote:See if this helps, it's a little thing I mocked up to get you going in the right direction. You just change my d:\wordconnect\ for your own folder with .jpgs in it:
@netmaestro, you're sample works fine, but if i manually change the filename so that the file doesn't exist the program still crashes like mine did!

@srod, I had tried ReadFile() but that didn't work, FileSize seems to work - thanks :)

i've found another problem now, with this code, once i click on the ImageGadget() a MessageRequester() is shown, but then if i mouseover the same gadget the MessageRequester() is shown again. why is this? it doesn't occur with a ButtonGadget()!

Code: Select all

If OpenWindow(1,0,0, 50,50,"Window") And CreateGadgetList(WindowID(1))
    ImageGadget(10, 0, 0, 100,100, 3)
Repeat
    EventID = WaitWindowEvent()

    If EventID = #PB_Event_Gadget
      ev = EventGadget()
      Select ev
        Case 10
          MessageRequester(ver$, Str(ev)) 
      EndSelect
    EndIf
    
  Until EventID = #PB_Event_CloseWindow
EndIf

End 

Posted: Sun May 14, 2006 10:39 pm
by srod
Try

Code: Select all

If OpenWindow(1,0,0, 50,50,"Window") And CreateGadgetList(WindowID(1)) 
    ImageGadget(10, 0, 0, 100,100, 3) 
Repeat 
    EventID = WaitWindowEvent() 

    If EventID = #PB_Event_Gadget And EventType()=#PB_EventType_LeftClick
      ev = EventGadget() 
      Select ev 
        Case 10 
          MessageRequester(ver$, Str(ev)) 
      EndSelect 
    EndIf 
    
  Until EventID = #PB_Event_CloseWindow 
EndIf 

End 

Posted: Sun May 14, 2006 10:45 pm
by netmaestro
LoadImage() won't crash your program on an unfound file unless the debugger is enabled. So a compiled exe will test LoadImage() and find 0 and you can handle that gracefully. But srod's suggestion, as well as my snippet, are just two ways you can do what you really must do - and that's ensure that the file exists before you bother trying to pull it in with LoadImage().

Was going to tell you you have to filter the eventtype but I see I don't have to now.

Posted: Sun May 14, 2006 10:50 pm
by lee__48
I didn't think of testing it as an executable. I just assumed the code was executed in the same way, whatever. Thanks.

Thanks again srod for a quick reply. Do you just need to filter the EventType for ImageButtons?

Regards,
Lee

Posted: Sun May 14, 2006 10:52 pm
by srod
No.

Posted: Sun Dec 17, 2006 2:14 pm
by PB
> a compiled exe will test LoadImage() and find 0 and you can handle that gracefully
[...]
> ensure that the file exists before you bother trying to pull it in with LoadImage()

My app needs to check for an image on disk, and if not there, create an image.
So my routine is as follows (cut down, of course). But if you try to run the app
with the Debugger on, you can't. So I always have to run my app WITHOUT
the Debugger, just because of the way LoadImage works. That's no good.

You said use the FileSize command on the file first, but why should I need to
wrap each LoadImage command with an If-FileSize-EndIf block, and also
therefore perform 2 x file operations instead of 1, just because the Debugger
doesn't like it? PureBasic v3.94 didn't do this, and just gave a return of 0 if
LoadImage couldn't load the image. So much simpler, no LoadImage wrapping,
and only 1 file operation. :) This is one situation where I do agree that v3.94
is better than v4.00.

Fred, are you sure this behaviour can't go back? Because it seems wrong that
we have to code extra lines (the FileSize check) when the final compiled exe
doesn't actually need them.

Code: Select all

If LoadImage(0,"C:\NonExistingImageFilenameHere.bmp")=0
  CreateImage(0,640,480) ; Can't do this because of LoadImage. :(
  Debug "Not loaded, so created instead"
Else
  Debug "Loaded okay"
EndIf