How to initialize an image?

Just starting out? Need help? Post your questions and find answers here.
tcoulon
New User
New User
Posts: 3
Joined: Wed May 12, 2021 9:04 pm

How to initialize an image?

Post by tcoulon »

Hello! I am a total beginner with purebasic (I do have some experience with tcl/tk and some RealBasic/Xojo).

I've been trying to learn by reading (PureBasic - A Beginners Guide.pdf and the PureBasic reference manual). I'm trying to use ButtonImageGadget but I keep getting "the specified #image is not initialized", however I don't see where I'm doing it wrong. From what I have seen/read I ended with this:

LoadImage(0, #PB_Compiler_Home + "smp1.png")
ButtonImageGadget(#PB_Any, 10, 10, 100, 100, ImageID(0))

I thought LoadImage was "initializing" the image. What am I missing?
(I'm using Linux, if that is important. The image file is in the purebasic directory (I thought this was the place indicated by #PB_Compiler_Home, at least a warning mark disappeared from the LoadImage line when I removed the path to the image file), but my test program is in the example directory.)
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: How to initialize an image?

Post by Saki »

Use at the first

UsePNGImageDecoder()
地球上の平和
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: How to initialize an image?

Post by eck49 »

I've been on this path myself, recently.

You can also incorporate an image into the executable program in a DataSection, which makes it independent of external files which might be misplaced, at the cost of a bigger executable, of course. There is a post elsewhere on the forum which relates to using such an entity....

https://www.purebasic.fr/english/viewto ... 11#p410611

and that post contains a link to another topic which gives one way of making the DataSection.

One DataSection can contain many items, each labelled (see the manual).

Bit of a slog, but the result (in Linux, like you) is lightning fast. Useful if you have many images to move around your window during the course of the program.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: How to initialize an image?

Post by Saki »

Copy the picture folder into your home directory.
This works not on Linux with Compiler Home.
地球上の平和
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: How to initialize an image?

Post by kenmo »

Verify that LoadImage() is succeeding

Code: Select all

Debug LoadImage(0, #PB_Compiler_Home + "smp1.png")
If not working (zero), check that the file path is correct

Code: Select all

Debug #PB_Compiler_Home + "smp1.png"
Debug FileSize(#PB_Compiler_Home + "smp1.png")
Like Saki said, make sure you put this before the load

Code: Select all

UsePNGImageDecoder()
tcoulon
New User
New User
Posts: 3
Joined: Wed May 12, 2021 9:04 pm

Re: How to initialize an image?

Post by tcoulon »

Thank you for your replies. I'll try them as soon as I have purebasic working again (for some reason installing it dragged nvidia stuff with it as screwed my system...)
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: How to initialize an image?

Post by Olli »

I think that near all is said... You can also add a filesize() test. But I am sure, once this load have been succeed, you won't meet this problem again.

1) To debug, first aspect : name and save your new basic file, even if you did not type anything !
--> [File] --> [Save as] --> type i.e. "mySourceFile.pb"

2) Name your image file.

Code: Select all

Define.S ImageName = GetCurrentDirectory() + "myImage.PNG"
3) Check if the file is detected by your program.

Code: Select all

If FileSize(ImageName) >= 0
 Debug "Image file detected"
Else
 Debug "Image file not detected"
EndIf
4) Don't forget the decoder library, as mentionned by Saki.

Code: Select all

UsePNDImageDecoder()
5) Test your load.

Code: Select all

If LoadImage(10, ImageName)
 Debug "Image detected"
Else
 Debug "Image not detected"
EndIf
6) Do not forget if the other function use directly the image identifyier (here 10), or the image handle, through ImageID(10).

Normally, this should be better. Try first to copy your image file near your source file (in the same directory).

Have a luck!
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: How to initialize an image?

Post by Saki »

Sometimes it is not easy to enter the correct path to a file.
You can simply have the path displayed like this,
then copy that and you're done.

Code: Select all

path$=OpenFileRequester("", "", "", 0)
Debug path$
SetClipboardText(path$) ; This put the path in your clipboard
地球上の平和
mariasbarker
New User
New User
Posts: 1
Joined: Mon May 24, 2021 8:22 am

Re: How to initialize an image?

Post by mariasbarker »

Thank you for your replies. I'll try them as soon as I have purebasic working again (for some reason installing it dragged nvidia stuff with it as screwed my system...)
Did you try using UsePNGImageDecoder() as suggested? Have you made it work? By the way I've also started with PureBasic - A Beginners Guide; still haven't gone so far with my learning yet...
tcoulon
New User
New User
Posts: 3
Joined: Wed May 12, 2021 9:04 pm

Re: How to initialize an image?

Post by tcoulon »

Sorry, not yet. Soon examination time here so I'm quite busy.
installing dependancies for PureBasic (seems nvidia stuff) screwed my system and I had to reinstall from backup.
As soon as I have time I'll install a system where I can run PureBasic and try all these suggestions.
Post Reply