• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 530
  • Last Modified:

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,
0
Akula
Asked:
Akula
  • 14
  • 14
  • 7
1 Solution
 
aeklundCommented:
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
0
 
Richie_SimonettiIT OperationsCommented:
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


0
 
Richie_SimonettiIT OperationsCommented:
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Richie_SimonettiIT OperationsCommented:
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
0
 
AkulaAuthor Commented:
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?
0
 
AkulaAuthor Commented:
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?
0
 
Richie_SimonettiIT OperationsCommented:
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
0
 
AkulaAuthor Commented:
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?
0
 
Richie_SimonettiIT OperationsCommented:
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
0
 
Richie_SimonettiIT OperationsCommented:
i have a question for you:
why do you need an imagelist?
0
 
AkulaAuthor Commented:
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?
0
 
aeklundCommented:
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.
0
 
AkulaAuthor Commented:
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
 
 
0
 
aeklundCommented:
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
0
 
Richie_SimonettiIT OperationsCommented:
did you paste all the code?
Take in mind that MY image list control has different name as yours ;)
0
 
AkulaAuthor Commented:
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
0
 
Richie_SimonettiIT OperationsCommented:
"...could you help me figure out how to get the CD dr..."!
see the link posted.
0
 
aeklundCommented:
1) Change the picture box to an image control and set the Stretch property to true.  This will zoom/stretch the picture to the size that you set the image control to.

so the code would change from:

  Picture1.Picture = LoadPicture(aFile(HScroll1.Value))
to
  Image1.Picture = LoadPicture(aFile(HScroll1.Value))
in all locations

2) Here is a routine to read the contents of that file into a text box:

Private Sub LoadTextFile(ByVal sPath As String, ByRef tb As TextBox)
  If Len(Dir(sPath)) = 0 Then Exit Sub
 
  tb.Text = ""
  Dim sLine As String
  Open sPath For Input As #1
    Do Until EOF(1)
      Line Input #1, sLine
      tb.Text = tb.Text & sLine & vbCrLf
    Loop
  Close #1
End Sub

add a text box to your form (Text1) and set the Multiline=True and Scrollbars = Both

Call the function above like this:

LoadTextFile "c:\catalog.txt", Text1

As far as the cd stuff, see Richie's post, or if that is too confusing, I could simplify it for you.
0
 
AkulaAuthor Commented:
Ahh...thanks Richie..did not see that the first time through.  
0
 
Richie_SimonettiIT OperationsCommented:
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...
0
 
AkulaAuthor Commented:
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,
0
 
AkulaAuthor Commented:
Actualy, Richie...I looked into your link again and I figured it out..thanks
0
 
Richie_SimonettiIT OperationsCommented:
glad to help.
0
 
aeklundCommented:
Richie-

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

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20548503.html

Go get em...
0
 
AkulaAuthor Commented:
aeklund...may I send you some e-mails with snipits of code for questions?
If so..send me a response to cbentley@dejarnette.com
0
 
AkulaAuthor Commented:
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,
0
 
Richie_SimonettiIT OperationsCommented:
you can store the filename as the key of images in imagelist as you can see in my code.
0
 
AkulaAuthor Commented:
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
0
 
aeklundCommented:
>>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.
0
 
Richie_SimonettiIT OperationsCommented:
<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?
0
 
Richie_SimonettiIT OperationsCommented:
aeklund: i did just realize that you posted "points for.." question and not Akula!!!
You are a gentleman.
:D
0
 
AkulaAuthor Commented:
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?

0
 
Richie_SimonettiIT OperationsCommented:
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
0
 
aeklundCommented:
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.
0
 
AkulaAuthor Commented:
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.

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 14
  • 14
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now