?
Solved

Imagelist

Posted on 2003-03-11
35
Medium Priority
?
493 Views
Last Modified: 2010-04-07
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
Comment
Question by:Akula
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 14
  • 7
35 Comments
 
LVL 10

Expert Comment

by:aeklund
ID: 8112883
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8112932
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8113015
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8113081
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
 
LVL 1

Author Comment

by:Akula
ID: 8113122
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
 
LVL 1

Author Comment

by:Akula
ID: 8113125
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8113173
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
 
LVL 1

Author Comment

by:Akula
ID: 8113193
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8113217
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8113240
i have a question for you:
why do you need an imagelist?
0
 
LVL 1

Author Comment

by:Akula
ID: 8113278
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
 
LVL 10

Expert Comment

by:aeklund
ID: 8113285
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
 
LVL 1

Author Comment

by:Akula
ID: 8113328
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
 
LVL 10

Expert Comment

by:aeklund
ID: 8113342
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8113405
did you paste all the code?
Take in mind that MY image list control has different name as yours ;)
0
 
LVL 1

Author Comment

by:Akula
ID: 8114319
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8114409
"...could you help me figure out how to get the CD dr..."!
see the link posted.
0
 
LVL 10

Accepted Solution

by:
aeklund earned 800 total points
ID: 8114434
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
 
LVL 1

Author Comment

by:Akula
ID: 8114439
Ahh...thanks Richie..did not see that the first time through.  
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8114529
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
 
LVL 1

Author Comment

by:Akula
ID: 8115287
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
 
LVL 1

Author Comment

by:Akula
ID: 8117086
Actualy, Richie...I looked into your link again and I figured it out..thanks
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8119171
glad to help.
0
 
LVL 10

Expert Comment

by:aeklund
ID: 8123225
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
 
LVL 1

Author Comment

by:Akula
ID: 8123353
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
 
LVL 1

Author Comment

by:Akula
ID: 8123780
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8127337
you can store the filename as the key of images in imagelist as you can see in my code.
0
 
LVL 1

Author Comment

by:Akula
ID: 8128786
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
 
LVL 10

Expert Comment

by:aeklund
ID: 8128831
>>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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8128840
<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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8129000
aeklund: i did just realize that you posted "points for.." question and not Akula!!!
You are a gentleman.
:D
0
 
LVL 1

Author Comment

by:Akula
ID: 8129165
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8129371
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
 
LVL 10

Expert Comment

by:aeklund
ID: 8129636
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
 
LVL 1

Author Comment

by:Akula
ID: 8129880
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question