用VB.NET制作图片浏览器时 ,打开图片文件时只能一张一张的打开,如何设置可以一次打开多个图片文件
2个回答
展开全部
Public Class Form1
Dim Files() As String
Dim ImgNum As Integer = 0
Dim CurIdx As Integer = 0
Sub OpenFiles()
InitialSet()
Try
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
If Not OpenFileDialog1.FileNames Is Nothing Then
Files = OpenFileDialog1.FileNames
Else
Files(0) = OpenFileDialog1.FileName
End If
ImgNum = Files.Length
Dim s As String = String.Format("你共打开 {0} 张图片", ImgNum)
Me.Text = s
StateSet()
CurIdx = 1
DisplayImage(CurIdx)
Else
StateSet()
Me.PictureBox1.Image = Nothing
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub InitialSet()
Me.OpenFileDialog1.Multiselect = True
Me.OpenFileDialog1.Filter = "(*.jpg)|*jpg"
Me.OpenFileDialog1.FilterIndex = 0
ImgNum = 0
CurIdx = 0
If Not Files Is Nothing Then
Array.Clear(Files, 0, Files.Length - 1)
End If
End Sub
Sub DisplayImage(ByRef Idx As Integer)
If IO.Path.GetExtension(Files(Idx - 1)).ToLower = ".jpg" Then
Me.PictureBox1.Load(Files(Idx - 1))
End If
StateSet()
End Sub
Sub StateSet()
If ImgNum > 1 Then
Me.btnNext.Enabled = True
Else
Me.btnNext.Enabled = False
Me.btnPervious.Enabled = False
Exit Sub
End If
If CurIdx > 1 And ImgNum > 1 Then
Me.btnPervious.Enabled = True
Else
Me.btnPervious.Enabled = False
End If
If CurIdx < ImgNum And ImgNum > 1 Then
Me.btnNext.Enabled = True
Else
Me.btnNext.Enabled = False
End If
End Sub
Sub NextIamge(ByRef Idx As Integer)
If Idx < ImgNum Then
Idx += 1
Else
Exit Sub
End If
DisplayImage(Idx)
End Sub
Sub PerviousIagme(ByRef Idx As Integer)
If Idx <> 1 Then
Idx -= 1
Else
Exit Sub
End If
DisplayImage(Idx)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Try
NextIamge(CurIdx)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
OpenFiles()
End Sub
Private Sub btnPervious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPervious.Click
Try
PerviousIagme(CurIdx)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Dim Files() As String
Dim ImgNum As Integer = 0
Dim CurIdx As Integer = 0
Sub OpenFiles()
InitialSet()
Try
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
If Not OpenFileDialog1.FileNames Is Nothing Then
Files = OpenFileDialog1.FileNames
Else
Files(0) = OpenFileDialog1.FileName
End If
ImgNum = Files.Length
Dim s As String = String.Format("你共打开 {0} 张图片", ImgNum)
Me.Text = s
StateSet()
CurIdx = 1
DisplayImage(CurIdx)
Else
StateSet()
Me.PictureBox1.Image = Nothing
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub InitialSet()
Me.OpenFileDialog1.Multiselect = True
Me.OpenFileDialog1.Filter = "(*.jpg)|*jpg"
Me.OpenFileDialog1.FilterIndex = 0
ImgNum = 0
CurIdx = 0
If Not Files Is Nothing Then
Array.Clear(Files, 0, Files.Length - 1)
End If
End Sub
Sub DisplayImage(ByRef Idx As Integer)
If IO.Path.GetExtension(Files(Idx - 1)).ToLower = ".jpg" Then
Me.PictureBox1.Load(Files(Idx - 1))
End If
StateSet()
End Sub
Sub StateSet()
If ImgNum > 1 Then
Me.btnNext.Enabled = True
Else
Me.btnNext.Enabled = False
Me.btnPervious.Enabled = False
Exit Sub
End If
If CurIdx > 1 And ImgNum > 1 Then
Me.btnPervious.Enabled = True
Else
Me.btnPervious.Enabled = False
End If
If CurIdx < ImgNum And ImgNum > 1 Then
Me.btnNext.Enabled = True
Else
Me.btnNext.Enabled = False
End If
End Sub
Sub NextIamge(ByRef Idx As Integer)
If Idx < ImgNum Then
Idx += 1
Else
Exit Sub
End If
DisplayImage(Idx)
End Sub
Sub PerviousIagme(ByRef Idx As Integer)
If Idx <> 1 Then
Idx -= 1
Else
Exit Sub
End If
DisplayImage(Idx)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Try
NextIamge(CurIdx)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
OpenFiles()
End Sub
Private Sub btnPervious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPervious.Click
Try
PerviousIagme(CurIdx)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询