Page 1 of 2

[Solved] Loading and using images

Posted: Fri Dec 09, 2016 4:30 pm
by Ajm
Hi,

Can someone explain to where I must store images that I want to load as part of my web application.

I have an image called background.png and some images that I want to use as toolbar icons.

I have tried storing the png's in a folder called Images in the same directory as the app but I can't seem to get them to load using the following code.

Code: Select all

Procedure LoadedBackground(Type, Filename$, ObjectId)
	OpenWindow(#MainWindow, 0, 0, #PB_Ignore, #PB_Ignore, "Test Window",#PB_Window_Background) 
	ImageGadget(#PB_Any, ((WindowWidth(#MainWindow)/2) - (ImageWidth(ObjectId)/2)), ((WindowHeight(#MainWindow)/2) - (ImageHeight(ObjectId)/2)), ImageWidth(ObjectId), ImageHeight(ObjectId), ImageID(ObjectId)) 
	FreeImage(ImageID(ObjectId))
EndProcedure

Procedure LoadingBackgroundError(Type, Filename$, ObjectId)
	Debug Filename$ + ": loading error"
EndProcedure

Procedure MainBackground()
	; Register the loading event before calling any resource load command
	BindEvent(#PB_Event_Loading, @LoadedBackground())
	BindEvent(#PB_Event_LoadingError, @LoadingBackgroundError())
	
	LoadImage(#LogoImage, "Images/background.png")
EndProcedure
In the debug window I always get Images/background.png: loading error reported from the LoadingBackgroundError() procedure.

Is there a special way that this has to be handled? I'm sure it's something simple that I'm missing but for the life of can't seem to figure it out.

Re: Loading and using images

Posted: Fri Dec 09, 2016 10:09 pm
by MrTAToad
Create a directory called Images and put your graphics there - this should be created from the project location. On a website it would be where the html and js files are uploaded.

So it would be something like

/file.html
/file.js
/Images/graphic1
/Images/graphic2

etc

Re: Loading and using images

Posted: Sat Dec 10, 2016 10:45 am
by tj1010
Your webserver, or a relative path when testing under SB and use /../ etc..

Re: Loading and using images

Posted: Sun Dec 11, 2016 3:54 pm
by Ajm
Hi,

I've tried every combination I can think of.

If I just set the LoadImage(#LogoImage, "background.png") and put the background.png file in the same directory as the html and js file it does still not load.

Could this be a bug in version 2?

Re: Loading and using images

Posted: Sun Dec 11, 2016 4:45 pm
by Basicoid
Not sure, just a try - have you checked upper and lower case, maybe this has to match as well? And just for a double check, have you checked the return value of LoadImage?

Re: Loading and using images

Posted: Sun Dec 11, 2016 5:59 pm
by Ajm
I've tried both lowercase, uppercase and mixed case.

The result I get from the LoadImage is [object Object] if I pass in a number to the LoadImage
If I pass in #PB_Any I get the result of 100000

I'm still at a loss to why this wont work.

Re: Loading and using images

Posted: Sun Dec 11, 2016 10:47 pm
by falsam
I tried the code with an image in the images folder and then the same image in the same folder as the code. I do not have a bug.

My code test.

Code: Select all

Enumeration 
  #MainWindow
  #LogoImage  
EndEnumeration

Procedure LoadedBackground(Type, Filename$, ObjectId)
  OpenWindow(#MainWindow, 0, 0, #PB_Ignore, #PB_Ignore, "Test Window",#PB_Window_Background) 
  ImageGadget(#PB_Any, ((WindowWidth(#MainWindow)/2) - (ImageWidth(ObjectId)/2)), ((WindowHeight(#MainWindow)/2) - (ImageHeight(ObjectId)/2)), ImageWidth(ObjectId), ImageHeight(ObjectId), ImageID(ObjectId)) 
  FreeImage(ImageID(ObjectId))
EndProcedure

Procedure LoadingBackgroundError(Type, Filename$, ObjectId)
  Debug Filename$ + ": loading error"
EndProcedure

Procedure MainBackground()
  ; Register the loading event before calling any resource load command
  BindEvent(#PB_Event_Loading, @LoadedBackground())
  BindEvent(#PB_Event_LoadingError, @LoadingBackgroundError())
  
  LoadImage(#LogoImage, "Images/background.png")
EndProcedure

MainBackground()

Re: Loading and using images

Posted: Mon Dec 12, 2016 11:58 am
by tj1010
Make sure they are loaded correctly is my best guess. I have a game in SB 2.0 that loads over 30 various sized bitmaps in alpha true color PNG and I haven't hit a bug. Wouldn't hurt to have a 2-3 failover for loading in case of network issues too.

Re: Loading and using images

Posted: Mon Dec 12, 2016 5:36 pm
by Basicoid
Tried your example and it worked fine here. As there is no filesize command available which would allow to check the presence of the file, what about the other way around?

Make a CreateFile statement and have a look where the file will be created then... :idea:

Re: Loading and using images

Posted: Mon Dec 12, 2016 7:55 pm
by Ajm
Thanks everyone for the suggestions. I'm unable to try this again now until Wednesday afternoon so I will try these and report back the results. I'm using Abyss web server on the Mac to serve the app so it could be something to do with the configuration of that as well.