Results 1 to 19 of 19

Thread: Laoding all pictures from a directory.

  1. #1

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535

    Exclamation

    Can anyone tell me how to load all the pictures (of same size) that are present in any directory on to a form?

  2. #2
    Addicted Member
    Join Date
    Oct 1999
    Location
    Oporto, Portugal
    Posts
    134
    1st make a dir "*.bmp" (for instance) to the directory where the images are and until the dir statement returns "" keep loading the images. You must create an array of pictureboxes.
    Jorge Ledo
    [email protected]
    Portugal were the sun allways shine... for programmers.

  3. #3
    Guest
    '******************************************
    'in the module declare following
    '******************************************
    Option Explicit

    Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
    End Type

    Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * 260
    cAlternate As String * 14
    End Type

    Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long

    Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" (ByVal hFindFile As _
    Long, lpFindFileData As WIN32_FIND_DATA) As Long
    Private Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" _
    (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long

    '*********************************************
    'on any event that you need, this is the code
    '**********************************************
    Dim hsearch As Long ' handle to the file search
    Dim findinfo As WIN32_FIND_DATA
    Dim success As Long
    Dim buffer As String
    Dim retval As Long
    Dim i As Integer
    dim DirPath as string

    ' Begin a file search:
    DirPath="Path to the directory that has your bmp files\"
    hsearch = FindFirstFile(DirPath & "*.bmp", findinfo)

    If hsearch = -1 Then
    exit sub
    End If

    Do ' begin loop
    buffer = Left(findinfo.cFileName, InStr(findinfo.cFileName, vbNullChar) - 1)

    i = i + 1
    Load Image1(i)
    Image1(i) = LoadPicture(DirPath & buffer)
    Image1(i).Top = Image1(i - 1).Top + Image1(i - 1).Width + 50
    Image1(i).Visible = True
    success = FindNextFile(hsearch, findinfo)
    Loop Until success = 0


    retval = FindClose(hsearch)

  4. #4
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Try something like this:
    Make your imagebox a control array.

    Code:
      'PURPOSE:List the Files in the selected directory
      Dim str_data As string
      dim int_Pic as integer
      
      str_data = Dir(strDir & "*.jpg")
      Do Until str_data = ""
        image1(x).picture = loadpicture(str_data)
        int_Pic = int_pic + 1
        str_data= Dir()
      Loop
    Chemically Formulated As:
    Dr. Nitro

  5. #5

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535

    Talking

    Thanks a lot HELENZAK and NITRO.

    Kinjal

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Wink

    If you're going outo resources/performance/RAM by creating too many pictureboxes, just ask me and I'll help you with bitblt
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535

    Question

    Hello HelenZak and Nitro,

    I tried both of yours code but none of them worked.
    HelenZak, your code is showing error "Function not defined" for FindFirstFile.
    Since I've never used this API I don't know it's syntax or what wrong. Please give the right code.

    And Nitro you code is showing file not found even though the file is present in the directory.


    Thanks a lot both of you.

    Kinjal

  8. #8
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Hello Kinjalgp!

    I bet you did not change the directory to your search directory.

    You might need to use CHDIR "C:\Your Directory".

    You can use the following to tell your current directory.

    msgbox CurDir()



    Please keep me posted, because I am challenge to fix my mistake.

    Thank You.
    Chemically Formulated As:
    Dr. Nitro

  9. #9

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535

    Unhappy

    No Nitro, I changed the directory but the program was showing the full directory path and along with the picture name and "Picture NOT FOUND".


    Kinjal

  10. #10
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    What about an extra "\"?

    Can you stick the entire string into a msgbox and check if it is correct. I really sounds like a path problem.


    You might have to change the "DO Loop" to a "For Loop" basing on the total number of Picturebox controls you have on your form.
    Chemically Formulated As:
    Dr. Nitro

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Load this into notepad and save it as a form. then run it and find your directory. You can modify it for your purpose

    VERSION 5.00
    Begin VB.Form Form1
    Caption = "Kedasus 5 minutes demo"
    ClientHeight = 4425
    ClientLeft = 60
    ClientTop = 345
    ClientWidth = 11130
    LinkTopic = "Form1"
    ScaleHeight = 4425
    ScaleWidth = 11130
    StartUpPosition = 3 'Windows Default
    Begin VB.DirListBox Dirc
    Height = 2790
    Left = 0
    TabIndex = 0
    Top = 1200
    Width = 1695
    End
    Begin VB.Image img
    Height = 855
    Index = 0
    Left = 120
    Top = 120
    Width = 855
    Visible = 0 'False
    End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Private Sub Dirc_Change()
    Path = Dir(Dirc.Path & String(1 + Int(Right(Dirc.Path, 1) = "\"), "\") & "*.jpg")
    Do While Len(Path)
    Load img(img.UBound + 1)
    With img(img.UBound)
    .picture = LoadPicture(Dirc.Path & String(1 + Int(Right(Dirc.Path, 1) = "\"), "\") & Path)
    .Visible = True
    .Stretch = True
    .Move img.UBound * 500, 0, 500, 500
    End With
    Path = Dir()
    Loop
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    Guest
    'This Declarations are used in Option Explicit of the form
    'If you need to use them in the Module You have to
    'use keyword Public before Type
    'and Public Before Daclare

    Option Explicit

    Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
    End Type

    Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * 260
    cAlternate As String * 14
    End Type

    Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long

    Declare Function FindNextFile Lib "kernel32.dll" Alias "FindNextFileA" (ByVal hFindFile As _
    Long, lpFindFileData As WIN32_FIND_DATA) As Long

    Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" _
    (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long

    '*********************************************
    'on any event that you need, this is the code
    'Pretend that this is cmdShow_Clik()
    '**********************************************
    'First Declare all local variables

    Dim hsearch As Long ' handle to the file search
    Dim findinfo As WIN32_FIND_DATA
    Dim success As Long
    Dim buffer As String
    Dim retval As Long
    Dim i As Integer
    dim DirPath as string

    ' Begin a file search:
    'If you have your picture files in "C:\MyPictures"
    'you have to use "\" that separates Directory from Files

    DirPath="C:\MyPictures\"
    'The Function FindFirstFile works with 2 parameters
    'first is a path of the file in your case it is DirPath & "*.bmp"
    'second is a user define type

    hsearch = FindFirstFile(DirPath & "*.bmp", findinfo)

    If hsearch = -1 Then
    exit sub
    End If

    Do ' begin loop
    buffer = Left(findinfo.cFileName, InStr(findinfo.cFileName, vbNullChar) - 1)

    i = i + 1
    Load Image1(i)
    Image1(i) = LoadPicture(DirPath & buffer)
    Image1(i).Top = Image1(i - 1).Top + Image1(i - 1).Width + 50
    Image1(i).Visible = True
    success = FindNextFile(hsearch, findinfo)
    Loop Until success = 0


    retval = FindClose(hsearch)

  13. #13
    Lively Member
    Join Date
    Jan 1999
    Location
    Rochester NY, USA
    Posts
    93
    In Nitro's code, you're going to have to do a couple things different. first, you're going to have to create the array.. that isn't done here. Then you're going to have to create each new element of the array as you get another picture. Then, where the line "image1(x).picture = loadpicture(str_data)" is.. change the (x) to (int_pic). "x" isn't defined here and what's going to happen is either the compiler is going to tell you that there's no "x" or even worse, you have a global var of "x" and it's going to use that one. Here's his code again.

    'PURPOSE:List the Files in the selected directory
    Dim str_data As string
    dim int_Pic as integer

    str_data = Dir(strDir & "*.jpg")
    Do Until str_data = ""
    image1(x).picture = loadpicture(str_data)
    int_Pic = int_pic + 1
    str_data= Dir()
    Loop


    *** Hope that points you in the right direction.

  14. #14

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    If you want I can send you my program. Give me your email address.
    I tried hard to make it run but it is getting the bitmaps name and giving error "X.bmp" not found. I don't know why it it saying so. If the program can find the file name why can't it load it?

    Kinjal

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Arrow

    You didn't try my sample?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    yes I tried your sample just now. Now I got what I wanted but what if my directory path is constant? Like (App.path & "\Bitmaps" & "\" & "*.bmp")?
    I tried to change the path in your code to a fixed under a command button but VB shows "Object required" and points at the path?


    Kinjal

  17. #17

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535

    Red face

    I tried your sample and is just what I wanted. But what if my directory path is constant like (App.Path & "\Bitmap" & "\" & "*.BMP"). I tried to make these changes in your code under command button but didn't work.

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well remove the filelistbox and replace those two:
    Code:
    Dirc.Path & String(1 + Int(Right(Dirc.Path, 1) = "\"), "\")
    with your own path and it should end with "\"
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  19. #19

    Thread Starter
    Fanatic Member kinjalgp's Avatar
    Join Date
    Apr 2000
    Location
    India
    Posts
    535
    What did you meant by replace those two ?
    I am not getting anything on my form and no errors when I replaced Direc.path to App.Path & "\Bitmap" & "\"* "*.bmp") ?????

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width