QB64 Phoenix Edition
Can images be read from a file into an array? - Printable Version

+- QB64 Phoenix Edition (https://staging.qb64phoenix.com)
+-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10)
+---- Thread: Can images be read from a file into an array? (/showthread.php?tid=1479)

Pages: 1 2


Can images be read from a file into an array? - PhilOfPerth - 02-17-2023

I have a folder containing several images (.jpg) that I want to place in an array, then pick any (or all) from that array to display. I don't see any appropriate commands that allow this; are there any? (simplicity is important to me!)  Wink


RE: Can images be read from a file into an array? - SMcNeill - 02-17-2023

Use File Listings to Arrays to put the filenames in an array.   https://staging.qb64phoenix.com/showthread.php?tid=68

Have the user select which files to view from those names.

Use an Array with _LOADIMAGE to load the users choices.

PUTIMAGE to screen as appropriate.


RE: Can images be read from a file into an array? - PhilOfPerth - 02-17-2023

(02-17-2023, 12:47 AM)SMcNeill Wrote: Use File Listings to Arrays to put the filenames in an array.   https://staging.qb64phoenix.com/showthread.php?tid=68

Have the user select which files to view from those names.

Use an Array with _LOADIMAGE to load the users choices.

PUTIMAGE to screen as appropriate.

Ahah, didn't look in _commands for image-related items, silly me!
On the trail now, thanks Steve.


RE: Can images be read from a file into an array? - TerryRitchie - 02-17-2023

(02-17-2023, 12:34 AM)PhilOfPerth Wrote: I have a folder containing several images (.jpg) that I want to place in an array, then pick any (or all) from that array to display. I don't see any appropriate commands that allow this; are there any? (simplicity is important to me!)  Wink

I do this all the time in my games. I use an array of LONG INTEGERS to preload images then call upon each array index when I need the images.

DIM Image(10) AS LONG

Image(0) = _LOADIMAGE("image001.jpg", 32)
Image(1) = _LOADIMAGE("image002.jpg", 32)
Image(2) = _LOADIMAGE("image003.jpg", 32)
Image(3) = _LOADIMAGE("image004.jpg", 32)
Image(4) = _LOADIMAGE("image005.jpg", 32)
etc..
etc..

You can assign descriptive variable names if you like as well:

WaterFall& = Image(0)
Forest& = Image(1)
SunSet& = Image(2)
etc..

_PUTIMAGE (0, 0), SunSet&
or
_PUTIMAGE (0, 0), Image(2)


Don't forget to free the images before your program terminates:

FOR i = 0 TO 10
    _FREEIMAGE Image(i)
NEXT i

Look at the code for any of my games and you'll see I use this method quite extensively.


RE: Can images be read from a file into an array? - 40wattstudio - 02-17-2023

(02-17-2023, 12:34 AM)PhilOfPerth Wrote: I have a folder containing several images (.jpg) that I want to place in an array, then pick any (or all) from that array to display. I don't see any appropriate commands that allow this; are there any? (simplicity is important to me!)  Wink

If your images have numbers for the filename you can also do it this way . . . (example is from my game but gives you the general idea).

Code: (Select All)
  CONST PLAYERDROP = 50 ' number of player animation frames       
  DIM SHARED dropplayer(PLAYERDROP) AS LONG ' player ship animations   
   
    LOCATE 3, 1: PRINT "loading opening animation"
    i = 1
    DO WHILE i <= PLAYERDROP
        drop$ = "SCRAPSHIP\x\gfx\playerdroppng\" + LTRIM$(STR$(i)) + ".png"
        dropplayer(i) = _LOADIMAGE(drop$)
        i = i + 1
    LOOP

Then afterwards use good ole _PUTIMAGE. Using an array like this is great for animations. 
See attached picture for an example of the folder contents that the array is iterating through.


RE: Can images be read from a file into an array? - PhilOfPerth - 02-17-2023

(02-17-2023, 01:17 AM)TerryRitchie Wrote:
(02-17-2023, 12:34 AM)PhilOfPerth Wrote: I have a folder containing several images (.jpg) that I want to place in an array, then pick any (or all) from that array to display. I don't see any appropriate commands that allow this; are there any? (simplicity is important to me!)  Wink

I do this all the time in my games. I use an array of LONG INTEGERS to preload images then call upon each array index when I need the images.

DIM Image(10) AS LONG

Image(0) = _LOADIMAGE("image001.jpg", 32)
Image(1) = _LOADIMAGE("image002.jpg", 32)
Image(2) = _LOADIMAGE("image003.jpg", 32)
Image(3) = _LOADIMAGE("image004.jpg", 32)
Image(4) = _LOADIMAGE("image005.jpg", 32)
etc..
etc..

You can assign descriptive variable names if you like as well:

WaterFall& = Image(0)
Forest& = Image(1)
SunSet& = Image(2)
etc..

_PUTIMAGE (0, 0), SunSet&
or
_PUTIMAGE (0, 0), Image(2)


Don't forget to free the images before your program terminates:

FOR i = 0 TO 10
    _FREEIMAGE Image(i)
NEXT i

Look at the code for any of my games and you'll see I use this method quite extensively.

Thanks Terry.
This looks exactly like what I'm trying to do.
But when I used this to build a simple model to work on, by typing this:

Dim Image(10) As Long
Image(0) = _LoadImage("RecPics/cat.jpg", 32)
Image(1) = _LoadImage("RecPics/cat.jpg", 32)
Image(2) = _LoadImage("RecPics/cat.jpg", 32)
Image(3) = _LoadImage("RecPics/cat.jpg", 32)
Image(4) = _LoadImage("RecPics/cat.jpg", 32)
_PutImage (0, 0), Image(2)
Sleep
_FreeImage (Image(2))

I get an "Illegal function call" error on line 7 (the _PutImage line).
(I have the cat.jpg image in a folder called RecPicS)

I'll have a look at some of your progs to see if i can spot what I'm doing wrong.


RE: Can images be read from a file into an array? - 40wattstudio - 02-17-2023

(02-17-2023, 01:53 AM)PhilOfPerth Wrote: Dim Image(10) As Long
Image(0) = _LoadImage("RecPics/cat.jpg", 32)
Image(1) = _LoadImage("RecPics/cat.jpg", 32)
Image(2) = _LoadImage("RecPics/cat.jpg", 32)
Image(3) = _LoadImage("RecPics/cat.jpg", 32)
Image(4) = _LoadImage("RecPics/cat.jpg", 32)
_PutImage (0, 0), Image(2)
Sleep
_FreeImage (Image(2))

I get an "Illegal function call" error on line 7 (the _PutImage line).
(I have the cat.jpg image in a folder called RecPicS)


I noticed you referenced RecPics/cat in the sample code above but then said your folder was RecPicS (note the capital S at the end). Probably a typo but pointing it out just in case.
And depending on how your program is structured you might have to use DIM SHARED Image(10) AS LONG for it to work properly


RE: Can images be read from a file into an array? - bplus - 02-17-2023

If Image(2) = -1 after _LoadImage, it couldn't find the image. You might need to set up a graphics screen for _Putimage.


RE: Can images be read from a file into an array? - SMcNeill - 02-17-2023

I can fix this one!  SuperSteve to the Save!

Code: (Select All)
SCREEN _NEWIMAGE(800,600,32) 'dont forget me!
Dim Image(10) As Long
Image(0) = _LoadImage("RecPics/cat.jpg", 32)
Image(1) = _LoadImage("RecPics/cat.jpg", 32)
Image(2) = _LoadImage("RecPics/cat.jpg", 32)
Image(3) = _LoadImage("RecPics/cat.jpg", 32)
Image(4) = _LoadImage("RecPics/cat.jpg", 32)
_PutImage (0, 0), Image(2)
Sleep
_FreeImage (Image(2))



You mention: "I get an "Illegal function call" error on line 7 (the _PutImage line)."

1 -- Dim Image(10) As Long
2 -- Image(0) = _LoadImage("RecPics/cat.jpg", 32)
3 -- Image(1) = _LoadImage("RecPics/cat.jpg", 32)
4 -- Image(2) = _LoadImage("RecPics/cat.jpg", 32)
5 -- Image(3) = _LoadImage("RecPics/cat.jpg", 32)
6 -- Image(4) = _LoadImage("RecPics/cat.jpg", 32)
7 -- _PutImage (0, 0), Image(2)

Only way that's line 7 is if that's the complete code, in which case, it's missing the call to work in a graphic screen. Big Grin


RE: Can images be read from a file into an array? - PhilOfPerth - 02-17-2023

(02-17-2023, 02:10 AM)SMcNeill Wrote: I can fix this one!  SuperSteve to the Save!

Code: (Select All)
SCREEN _NEWIMAGE(800,600,32) 'dont forget me!
Dim Image(10) As Long
Image(0) = _LoadImage("RecPics/cat.jpg", 32)
Image(1) = _LoadImage("RecPics/cat.jpg", 32)
Image(2) = _LoadImage("RecPics/cat.jpg", 32)
Image(3) = _LoadImage("RecPics/cat.jpg", 32)
Image(4) = _LoadImage("RecPics/cat.jpg", 32)
_PutImage (0, 0), Image(2)
Sleep
_FreeImage (Image(2))

Bingo!
Steve wins the choccy frog! I was missing the _NewImage line. And there's my pussycat!
Now, I just have to select it (easy), size it (ok...), and position it (hmm...)
i think I'll be ok now. Thanks all for the help.  Big Grin  


You mention: "I get an "Illegal function call" error on line 7 (the _PutImage line)."

1 -- Dim Image(10) As Long
2 -- Image(0) = _LoadImage("RecPics/cat.jpg", 32)
3 -- Image(1) = _LoadImage("RecPics/cat.jpg", 32)
4 -- Image(2) = _LoadImage("RecPics/cat.jpg", 32)
5 -- Image(3) = _LoadImage("RecPics/cat.jpg", 32)
6 -- Image(4) = _LoadImage("RecPics/cat.jpg", 32)
7 -- _PutImage (0, 0), Image(2)

Only way that's line 7 is if that's the complete code, in which case, it's missing the call to work in a graphic screen.  Big Grin
 Yep, as Steve pointed out, I needed the _NewImage bit to make it work. That was the complete code for experimenting on. Thanks.