VB遍历文件夹中的图片

新建一个工程:在form1 中添加一个时间控件 和一个Image控件要求就是将下面路径文件夹中的图片每间隔1秒就显示一张图片到Image控件中 并且可以循环显示d:\123\图片文件夹 文件夹中的图片不知道名字 不知道数量 只知道是.JPG格式的图片文件能否给段代码 最好有注释
2026-12-04 20:19:14
推荐回答(2个)
回答1:

'一个窗体,一个list,一启动就会自动把给定的目录里所有的jpg文件读出,并添加到list中。by fallx
Private Sub Form_Load()
On Error GoTo errEvent
Dim strFileName() As String
Dim strPath As String
Dim intCount, i As Long
Dim strFile As String
intCount = 0
strPath = "D:\相片\" & "*.jpg" '这个目录可以改为用 打开文件夹给出,*.jpg限定文件类型
strFile = Dir(strPath)
While strFile <> "" '搜索给定目录下的所有该类型文件
DoEvents
intCount = intCount + 1
ReDim Preserve strFileName(intCount) As String '重定义数组
strFileName(intCount - 1) = strFile '把文件名存到数组中
strFile = Dir '再取下一个
Wend
For i = 0 To intCount - 1
List1.AddItem strFileName(i) & ".jpg" '将文件名插入listbox的里面
Next i
Exit Sub
errEvent:
MsgBox "非常抱歉,出错,部分或全部图片无法读取!"
End Sub

回答2:

添加文件列表控件,filelist
Dim Shunxu As Integer, zongshu As Integer, strfilename As String
Private Sub Form_Load()
File1.Path = "d:\" ' "d:\123\图片文件夹" '文件夹路径
File1.Pattern = "*.gif" '文件格式
Timer1.Interval = 1000
Timer1.Enabled = True
zongshu = File1.ListCount '总数
Shunxu = 0
End Sub

Private Sub Timer1_Timer()
If zongshu > 0 Then
strfilename = File1.List(Shunxu) '图片文件名
Image1.Picture = LoadPicture(File1.Path + "\" + strfilename) '图片文件路径
Shunxu = Shunxu + 1
If Shunxu = zongshu - 1 Then
Shunxu = 0
End If
End If
End Sub