[Solved] Loading and using images

Just starting out? Need help? Post your questions and find answers here.
Ajm
Posts: 31
Joined: Wed Aug 26, 2015 1:52 pm

[Solved] Loading and using images

Post 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.
Last edited by Ajm on Wed Dec 14, 2016 6:13 pm, edited 2 times in total.
Regards

Andy
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

Re: Loading and using images

Post 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
tj1010
Posts: 218
Joined: Wed May 27, 2015 1:36 pm
Contact:

Re: Loading and using images

Post by tj1010 »

Your webserver, or a relative path when testing under SB and use /../ etc..
Ajm
Posts: 31
Joined: Wed Aug 26, 2015 1:52 pm

Re: Loading and using images

Post 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?
Regards

Andy
Basicoid
Posts: 12
Joined: Thu Dec 08, 2016 4:57 pm

Re: Loading and using images

Post 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?
Ajm
Posts: 31
Joined: Wed Aug 26, 2015 1:52 pm

Re: Loading and using images

Post 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.
Regards

Andy
falsam
Posts: 286
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Re: Loading and using images

Post 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()

➽ Windows 11 - jdk-11.0.2 - SB 3.00 - Android 15
https://falsam.com

Sorry for my poor english
tj1010
Posts: 218
Joined: Wed May 27, 2015 1:36 pm
Contact:

Re: Loading and using images

Post 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.
Basicoid
Posts: 12
Joined: Thu Dec 08, 2016 4:57 pm

Re: Loading and using images

Post 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:
Ajm
Posts: 31
Joined: Wed Aug 26, 2015 1:52 pm

Re: Loading and using images

Post 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.
Regards

Andy
Post Reply