Link to home
Start Free TrialLog in
Avatar of Akula
Akula

asked on

Imagelist

I would like to do the following, any help would be greatly appreciated.
I have a CD with JPEG images on it numbered 1.jpeg to x.jpeg.
I would like the form to scan the CD (at form load) and then create the Imagelist at that point so I can scroll through the images via the mouse scroll. Also, when I put in another CD, I would like to press a button to re-scan the CD and the re-popluate the Imagelist.
I can do the mouse scroll later...I just want to be able to scan the cd and then popluate the imagelist right now.

Thanks in advance,
Avatar of aeklund
aeklund

This should do it for you... Please let me know if there is anything that you don't understand.

NOTE: Make sure you have Microsoft Scripting Runtime referenced under Project/References

Paste into a new form:

Option Explicit
Dim oFso As FileSystemObject

Private Sub Form_Load()
  Set oFso = New FileSystemObject
   
    Debug.Print "Files Searched: " & GetImages("l:\docs")
    Debug.Print "Images Found: " & ImageList1.ListImages.Count
 
  Set oFso = Nothing
End Sub

Private Function GetImages(ByVal sSource As String) As Long
  If Right(sSource, 1) <> "\" Then sSource = sSource & "\"
  If oFso.FolderExists(sSource) Then
    Dim lCount As Long
   
    Dim oFile As File
    For Each oFile In oFso.GetFolder(sSource).Files
      If Right(UCase(oFile.Name), 3) = "JPG" Then
        Debug.Print oFile.Path
        ImageList1.ListImages.Add , , LoadPicture(oFile.Path)
      End If
    Next oFile
   
    'Do Recursive
    Dim oFolder As Folder
    For Each oFolder In oFso.GetFolder(sSource).SubFolders
      lCount = lCount + GetImages(sSource & oFolder.Name & "\")
    Next oFolder

    lCount = lCount + oFso.GetFolder(sSource).Files.Count
  End If
  GetImages = lCount
End Function
Avatar of Richie_Simonetti
Replace the path for your cd letter:

VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.PictureBox Picture1
      Height          =   2355
      Left            =   60
      ScaleHeight     =   2295
      ScaleWidth      =   3555
      TabIndex        =   1
      Top             =   60
      Width           =   3615
   End
   Begin VB.CommandButton Command1
      Caption         =   "Command1"
      Height          =   495
      Left            =   3240
      TabIndex        =   0
      Top             =   2580
      Width           =   1215
   End
   Begin MSComctlLib.ImageList imglist1
      Left            =   900
      Top             =   300
      _ExtentX        =   1005
      _ExtentY        =   1005
      BackColor       =   -2147483643
      MaskColor       =   12632256
      _Version        =   393216
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Command1_Click()
Static i As Long

If imglist1.ListImages.Count < i Then
    i = 1
Else
    i = i + 1
End If
Set Picture1.Picture = imglist1.ListImages(i).Picture

End Sub

Private Sub Form_Load()
Dim strFile As String
Dim sPath As String

sPath = "E:\Documents and Settings\rsimonetti\Mis documentos\Mis imágenes\amber\"
strFile = Dir$(sPath & "*.jpg", vbArchive)
Do While strFile <> ""
    imglist1.ListImages.Add , strFile, LoadPicture(sPath & strFile)
    strFile = Dir$()
Loop
End Sub


sorry, a little modification:
Private Sub Command1_Click()
Static i As Long

If imglist1.ListImages.Count = i Then
    i = 1
Else
    i = i + 1
End If
Set Picture1.Picture = imglist1.ListImages(i).Picture

End Sub
Avatar of Akula

ASKER

Richie...I can not get yours to work,
Aeklund...yours appears to work but I get an our of memory error.  Could I possibly make the directory output to a file, and then have the imagelist call from that file?
Avatar of Akula

ASKER

Richie...I can not get yours to work,
Aeklund...yours appears to work but I get an our of memory error.  Could I possibly make the directory output to a file, and then have the imagelist call from that file?
another one:
Private Sub Command1_Click()
Static i As Long
With imglist1
    If i >= .ListImages.Count Then
        i = 1
    Else
        i = i + 1
    End If
    Set Picture1.Picture = .ListImages(i).Picture
    Me.Caption = .ListImages.Item(i).Key
End With

End Sub
Avatar of Akula

ASKER

Richie...I can not get yours to work,
Aeklund...yours appears to work but I get an our of memory error.  Could I possibly make the directory output to a file, and then have the imagelist call from that file?
Why?, this was a test program you have to save the contents of it in a new text file and...
never mind. Do this:
Add a picture control and a command button to one form and paste this inside declarations section of it:


Private Sub Form_Load()
Dim strFile As String
Dim sPath As String
' change this
sPath = "E:\Documents and Settings\rsimonetti\Mis documentos\Mis imagenes\amber\"
strFile = Dir$(sPath & "*.jpg", vbArchive)
Do While strFile <> ""
   imglist1.ListImages.Add , strFile, LoadPicture(sPath & strFile)
   strFile = Dir$()
Loop
End Sub

Private Sub Command1_Click()
Static i As Long
With imglist1
   If i >= .ListImages.Count Then
       i = 1
   Else
       i = i + 1
   End If
   Set Picture1.Picture = .ListImages(i).Picture
   Me.Caption = .ListImages.Item(i).Key
End With

End Sub
i have a question for you:
why do you need an imagelist?
Avatar of Akula

ASKER

Richie...I can not get yours to work,
Aeklund...yours appears to work but I get an our of memory error.  Could I possibly make the directory output to a file, and then have the imagelist call from that file?
Akula-

You probably run out of memory because the imagelist is being populated with all these "jpg" files thus causing your out of memory error.

To solve this, you would need only load the image on demand while your scrolling through them.  You could gather all the paths into an array, then when you view them just load each picture individually upon demand from the array.
Avatar of Akula

ASKER

Ritchie, I thought I needed the imagelist to be able to scroll through  list of images.  Am I mistaken?
Also on your last bit of code I get an error when i press the button, it states: Object Required and when I press debug it highlites this line in the command_click:

  If i >= .ListImages.Count Then
 
 
In order to accomplish this without an imagelist, I provided a complete sample using:
 1 - picture box (Picture1)
 1 - hscroll (HScroll1)

Paste into a form:

Option Explicit
Dim oFso As FileSystemObject

Dim lCurrent As Long
Dim lFile As Long
Dim aFile() As String


Private Sub Form_Load()
  Set oFso = New FileSystemObject
    lFile = 0
    Debug.Print "Files Searched: " & GetImages("l:\docs")
    HScroll1.Min = 0
    HScroll1.Max = lFile - 1
    Picture1.Picture = LoadPicture(aFile(HScroll1.Value))
  Set oFso = Nothing
End Sub

Private Function GetImages(ByVal sSource As String) As Long
 If Right(sSource, 1) <> "\" Then sSource = sSource & "\"
 If oFso.FolderExists(sSource) Then
   Dim lCount As Long
   
   Dim oFile As File
   For Each oFile In oFso.GetFolder(sSource).Files
     If Right(UCase(oFile.Name), 3) = "JPG" Then
       Debug.Print oFile.Path
       ReDim Preserve aFile(lFile + 1)
       aFile(lFile) = oFile.Path
       lFile = lFile + 1
     End If
   Next oFile
   
   'Do Recursive
   Dim oFolder As Folder
   For Each oFolder In oFso.GetFolder(sSource).SubFolders
     lCount = lCount + GetImages(sSource & oFolder.Name & "\")
   Next oFolder

   lCount = lCount + oFso.GetFolder(sSource).Files.Count
 End If
 GetImages = lCount
End Function

Private Sub HScroll1_Change()
  Picture1.Picture = LoadPicture(aFile(HScroll1.Value))
End Sub
did you paste all the code?
Take in mind that MY image list control has different name as yours ;)
Avatar of Akula

ASKER

aeklund, that works great for me....two small questions.
1.)  The Jpeg images are different sizes, I would like for them to "stay" in the windowfor the picure box.. i have played around with it's properties, but It won't do it.
It will have to scle the imge down so to speak to fit in window..this should be easy to do..right?

2.)I have a file called catalog.txt on the CD. i would like for it's information to appear on the form.
I tried to run a open to print it to a text1 box...but nothing appears.  Can you help.

And finally for 75 more points...could you help me figure out how to get the CD drive.  Point: if they have 2 cd drives I need it to scan for ..say catalog.txt then that is the cd with images.

Thanks
"...could you help me figure out how to get the CD dr..."!
see the link posted.
ASKER CERTIFIED SOLUTION
Avatar of aeklund
aeklund

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Akula

ASKER

Ahh...thanks Richie..did not see that the first time through.  
If you have more than one cd drive...
when a cdrom is found do a dir$ to see if it has txt file, if not continue the loop until found next cd and do the same dir$ fucntion and so on...
Avatar of Akula

ASKER

aeklund, I figured out the textbox rather easily, but I am stuck on one small part now but I will work it out. (Basically the catalog.txt has this "images/00001.jpg | Smith, John | SSN | CT" and I want to make three labels with Name, SSN, Modality) Right now I have one Label that I have been able to replace the unnecessary stings with blanks so it is working to an extent now.

I followed Richie's link and I am getting another error.
Could you simplify the code to find the cdrom..my plan is to get the cdrom drive that has the catlog.txt and place it in the drive letter of the txt file for the imagebox.
BTW, the imagebox worked perfectly for me.
Thanks,
Avatar of Akula

ASKER

Actualy, Richie...I looked into your link again and I figured it out..thanks
glad to help.
Richie-

Thanks for your effert, you did help out and want to make sure you get your share of points...

https://www.experts-exchange.com/questions/20548503/Points-for-Richie-Simonetti.html

Go get em...
Avatar of Akula

ASKER

aeklund...may I send you some e-mails with snipits of code for questions?
If so..send me a response to cbentley@dejarnette.com
Avatar of Akula

ASKER

aeklund...could yoou help me with this code once more?
I would like to have a lable display the file name of each image as you scroll.  Can this be done easily from your existing code?
Thanks in advance,
you can store the filename as the key of images in imagelist as you can see in my code.
Avatar of Akula

ASKER

Richie, I like yoour idea...so I created a new form using your last bit of code...2 things....it runs out of memory and I would like to scroll through the images..along with your last comment to store the filename as key of images.
If yoou can get me to where I need to be I will get you another 75 points.
Thanks
>>I would like to have a lable display the file name of each image as you scroll.  Can this be done easily from your existing code?

yes, just add a label (label1) and set the caption during the scroll event:

  label1.caption = aFile(HScroll1.Value)

and also add it on the form load event to set the initial label to the first picture.
<Out of memory> Well, how many images do you have?
Maybe, imagelist doesn't likes too much images 8i don't know how much it could supports, just AFAIK, imagelist is used for little images as icons).
But as i told you before, why do you need an extra (and heavy) control to do your thumbnail?
aeklund: i did just realize that you posted "points for.." question and not Akula!!!
You are a gentleman.
:D
Avatar of Akula

ASKER

Richie, I have anywhere from 10 pictures to as many as 1500 images.  So yoou are right...the imagelist probably cannot work for me.  I will use your code on another idea for another program. So you may here from me soon.

Aeklund..that worked great...now for the save as button.
Can we send e-mails back and forth on this?

I think you could use an array of stdPicture objects or a collection (which is less efficient but has its advantages too) to store those images in memory (if you need a fast way) or use Loadpicture and read CD over and over again for every picture.
If you use an array, it would read th eimages just ONCE and, if for some reason, you go back and for, images are already readed so no access to CD anymore.
Cheers
richie:  just wanted to give credit where credit was due.

akula: I do not to give out my email address, if I did I would be flooded with questions.  I feel it is best to correspond through EE.

I wish I could help everyone, but don't have the manpower to do so.  If you post on EE, and I'm not available, your question will not get unheard and others will respond.

I think this question is resolved, so thanks for the points and hope everything works out.  If you any other further questions, I recommend posting them in a new feed and make sure to initially list specifically what each question is.  Some people don't like it when you post one question, then later down in the feed, you keep asking more and more.

Glad to be of assistance, and enjoy.
Avatar of Akula

ASKER

Aeklund,
Thank you for the insight..this is only my second question ever asked here.
I was unaware of the protocols.  Thanks for your help and god speed.