Selecting the Image to display in the BinaryFile

batninja

Member
Licensed User
Hi all
I hope this one is not too obvious, but I cannot see how to select the image to retrieve from a BinaryFile. My code just gets each image in sequence but I want to be able to go forwards and backwards and get images based on the position in the file.

My code is

Sub App_Start
'FileOpen(c1,"data.dat",cRandom)
'bin.New1(c1,true)
'bin.EmbedFile (AppPath & "\build.jpg")
'bin.EmbedFile (AppPath & "\wall.jpg")
'bin.EmbedFile (AppPath & "\garden.jpg")
'FileClose(c1)


Form1.Show
FileOpen(c1,"data.dat",cRandom)
bin.New1(c1,true)
'this get the first image in the sequence
Image1.Image = bin.RetrieveImage
End Sub

Sub Num1_ValueChanged
'and then this gets the next but how do I get a specific image
'or get a previous image?

Image1.Image = bin.RetrieveImage
End Sub

Sub Form1_Close
FileClose(c1)
End Sub

Any ideas?
Thanks - Bats
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
I'm not all that clued up on this myself but I think you need to add all the images it an imagelist control and then you will be able to select which ever one you want.

Regards,
RandomCoder.
 

dlfallen

Active Member
Licensed User
Longtime User
I've wrestled with this quite a bit. See the Image Explorer post under "Share Your Creations" on this forum. There is a program (with source code) that will scroll back and forth among BMP files stored in a binary file. I have updated this program to handle all supported graphic file types, and will post the final version on the Forum sometime this weekend.

Basically, you have two choices. One, as mentioned by RandomCoder, is to read all the images into an ImageList control. The other is to access an arbitrary image file by specifying its starting location before issuing the RetrieveImage command. The Image Explorer program mentioned above is a tool that can give you that information.

-dlfallen
 

batninja

Member
Licensed User
Cool thanks

Hey guys

Thanks for the tips, I'll try check them out.

I did think of another "brute force" method using the RetrieveFile command to export the required image out as say "CurrentImage.jpg" and then reimporting it back into the required location.

Bats
 

batninja

Member
Licensed User
The ImageList Control works a treat

Tried out the ImageList control and its great, just need to work out a way of determining how many images are in the file

Sub App_Start
'FileOpen(c1,"data.dat",cRandom)
'bin.New1(c1,true)
'bin.EmbedFile (AppPath & "\build.jpg")
'bin.EmbedFile (AppPath & "\wall.jpg")
'bin.EmbedFile (AppPath & "\garden.jpg")
'FileClose(c1)

Form1.Show
FileOpen(c1,"data.dat",cRandom)
bin.New1(c1,true)
for x = 0 to 2
ImageList1.Add(bin.RetrieveImage)
next x
Image1.Image = imagelist1.Item(0)
End Sub

Sub Num1_ValueChanged
Image1.Image = imagelist1.Item(num1.Value)
End Sub

Sub Form1_Close
FileClose(c1)
End Sub
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Instead of the For Next loop you could try a Do Until.
Once again I've not done this myself (I've not had any need yet), but I'd expect you to either receive an EOF or Error which could be used as the signal to drop out of the loop.

Regards,
RandomCoder.
 

dlfallen

Active Member
Licensed User
Longtime User
Actually, what usually happens when you try to retrieve a file past the end of the binary file is the program crashes. What I do in my Image Explorer program is check the file positon after each image retrieval. It the image position + 32 is greater than the file size
FileSize(OpenDialog1.File)
then I know I have read the last image in the file.

I have posted the final version of Image Explorer in the Share Your Creations section. The source code there illustrates the method.

-dlfallen
 

batninja

Member
Licensed User
Thanks for the Do While tip works great, note I had to check for the file length less 40

Sub App_Start
'FileOpen(c1,"data.dat",cRandom)
'bin.New1(c1,true)
'bin.EmbedFile (AppPath & "\build.jpg")
'bin.EmbedFile (AppPath & "\wall.jpg")
'bin.EmbedFile (AppPath & "\garden.jpg")
'FileClose(c1)

Form1.Show
FileOpen(c1,"data.dat",cRandom)
bin.New1(c1,true)
Do While bin.Position < bin.Length-40
msgbox("position = " & bin.Position & " length = " & bin.Length)
ImageList1.Add(bin.RetrieveImage)
Loop
Image1.Image = imagelist1.Item(0)
End Sub
 
Top