QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: vince on August 06, 2021, 06:54:10 am

Title: Problem with file path
Post by: vince on August 06, 2021, 06:54:10 am
I am scratching my head with this very simple issue (possibly an issue on my part). I thought that when a file of any kind is referenced from your compiled program that it is relative to where your .EXE file is. I have run the following bit of code and it keeps erroring when it gets to the _loadimage line. The file is in the same directory as the .EXE and it is spelled correctly. I have even tried to use the full path to load the file, but that also fails.

DIM i AS LONG

i = _LOADIMAGE("as.png")
IF i < -1 THEN PRINT "Error loading image..": END
_PUTIMAGE (0, 0), i

I just keep getting the "Error loading image.." message

Call me stupid, I don't mind, as long as I can get past this.
Title: Re: Problem with file path
Post by: SMcNeill on August 06, 2021, 08:14:33 am
Can you share the image file?  Is it perhaps saved in a more modern version/format than our old code can recognize?  I don’t *think* the png format has had any new revisions for a while, but I wouldn’t swear to anything.
Title: Re: Problem with file path
Post by: SpriggsySpriggs on August 06, 2021, 08:38:01 am
I think your problem is because you are actually loading it correctly but handling the logic incorrectly. According to the Wiki for _LOADIMAGE:

Quote
Valid LONG handle returns are less than -1 (handle& < -1).

So you are getting a valid handle but telling yourself that it is invalid.
Title: Re: Problem with file path
Post by: FellippeHeitor on August 06, 2021, 09:24:01 am
Yeah, that's correct. A handle = -1 or 0 indicates failure to load. Change it to this:

Code: QB64: [Select]
  1.  
  2. i = _LOADIMAGE("as.png")
  3. IF i >= -1 THEN PRINT "Error loading image..": END
  4. _PUTIMAGE (0, 0), i
Title: Re: Problem with file path
Post by: bplus on August 06, 2021, 10:16:59 am
Under Run menu "Output EXE to Source Folder" is bulleted?

No, I guess that gets a different error.
Title: Re: Problem with file path
Post by: bplus on August 06, 2021, 10:27:57 am
Try this:
Code: QB64: [Select]
  1. Screen _NewImage(800, 600, 32)
  2. i = _LoadImage("as.png")
  3. If i >= -1 Then Print "Error loading image..": End
  4. _PutImage (0, 0), i
  5.  
  6.  
  7.  
Title: Re: Problem with file path
Post by: SpriggsySpriggs on August 06, 2021, 10:42:42 am
@bplus
Try this:
Code: QB64: [Select]
  1. Screen _NewImage(800, 600, 32)
  2. i = _LoadImage("as.png")
  3. If i >= -1 Then Print "Error loading image..": End
  4. _PutImage (0, 0), i
  5.  

That looks suspiciously similar to Fellippe's answer, lol ;)
Yeah, that's correct. A handle = -1 or 0 indicates failure to load. Change it to this:

Code: QB64: [Select]
  1.  
  2. i = _LOADIMAGE("as.png")
  3. IF i >= -1 THEN PRINT "Error loading image..": END
  4. _PUTIMAGE (0, 0), i
Title: Re: Problem with file path
Post by: bplus on August 06, 2021, 10:44:23 am
@bplus
That looks suspiciously similar to Fellippe's answer, lol ;)
Yeah but Fellippes errors out and mine doesn't because of the screen line.

The error was coming from line 3.
Title: Re: Problem with file path
Post by: FellippeHeitor on August 06, 2021, 10:49:21 am
I never assumed this was the full code, but a snippet removed from another piece of code. My reply relies solely on the fact that he was checking for the handle incorrectly, as Zach had pointed out. Using _LOADIMAGE without passing the mode in the second parameter without being in graphical mode will indeed error.
Title: Re: Problem with file path
Post by: bplus on August 06, 2021, 11:00:14 am
OP:
Quote
I have run the following bit of code and it keeps erroring when it gets to the _loadimage line.
Title: Re: Problem with file path
Post by: SpriggsySpriggs on August 06, 2021, 11:03:12 am
The error he was getting was not a QB64 error.

Quote
I just keep getting the "Error loading image.." message

That error is one that he made himself.
Title: Re: Problem with file path
Post by: bplus on August 06, 2021, 11:03:43 am
BTW this will error out on line 5, the graphics command if screen has not been set up for graphics:
Code: QB64: [Select]
  1.  
  2. i = _LoadImage("as.png", 32)
  3. If i >= -1 Then Print "Error loading image..": End
  4. _PutImage (0, 0), i
  5.  
Title: Re: Problem with file path
Post by: SpriggsySpriggs on August 06, 2021, 11:05:01 am
I'm not doubting that it will create an error. But like he was originally saying, the error was "Error loading image..", which is an error that he wrote himself to display for invalid handles but he was checking the handle incorrectly.
Title: Re: Problem with file path
Post by: bplus on August 06, 2021, 11:11:28 am
Yeah misdiagnosed problems are bane of my programming experience.
Title: Re: Problem with file path
Post by: vince on August 06, 2021, 11:21:43 am
I now know there was a logic error with the IF statement (my bad, thanks Spriggs).

Once I made that change I was still getting an error with the _putimage (Illegal function call). I changed the SCREEN statements mode to 32 (noticed bplus had that in the SCREEN statement) and it worked OK. So there were a couple of problems there.

Thanks for all your responses, appreciated.