Can anyone tell me how to load all the pictures (of same size) that are present in any directory on to a form?
Printable View
Can anyone tell me how to load all the pictures (of same size) that are present in any directory on to a form?
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.
'******************************************
'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)
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
Thanks a lot HELENZAK and NITRO.
Kinjal
If you're going outo resources/performance/RAM by creating too many pictureboxes, just ask me and I'll help you with bitblt
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
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.
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
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.
Load this into notepad and save it as a form. then run it and find your directory. You can modify it for your purpose :D
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
'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)
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.
:p 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? :p
Kinjal
You didn't try my sample?
:D 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? :confused:
Kinjal
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.
Well remove the filelistbox and replace those two:
with your own path and it should end with "\"Code:Dirc.Path & String(1 + Int(Right(Dirc.Path, 1) = "\"), "\")
:confused: 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") ?????