我被困住了,不知道该怎么办,我正在尝试制作一个image viewer
,并将image
的所有路径按文件夹放在一个listbox
中,然后当我尝试在index 2
列表中选择时,我得到错误,它说"内存不足异常">
这是我的代码
Public Class Form1
Dim myimg As Array
Private Sub img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles img.Click
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
myimg = IO.Directory.GetFiles(FolderBrowserDialog1.SelectedPath)
ListBox1.Items.AddRange(myimg)
ListBox1.SelectedIndex = 0
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.Refresh()
Me.BackgroundImage = Nothing
Me.BackgroundImage = Image.FromFile(ListBox1.SelectedItem.ToString)
End Sub
Private Sub min_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles min.Click
Me.WindowState = FormWindowState.Maximized
End Sub
Private Sub max_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles max.Click
Me.WindowState = FormWindowState.Normal
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
错误位于此处
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.Refresh()
Me.BackgroundImage = Nothing
Me.BackgroundImage = Image.FromFile(ListBox1.SelectedItem.ToString)
End Sub
,请帮忙谢谢:)
列表中可能没有有效的图像文件。 查找异常:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles ListBox1.SelectedIndexChanged
Try
If ListBox1.SelectedIndex > -1 Then
Me.BackgroundImage = Image.FromFile(ListBox1.SelectedItem.ToString)
End If
Catch ex As OutOfMemoryException
'not a valid image file
End Try
End Sub
要筛选列表,您可以执行以下操作:
Using fbd As New FolderBrowserDialog
If fbd.ShowDialog = DialogResult.OK Then
myimg = IO.Directory.GetFiles(fbd.SelectedPath).Where( _
Function(x) x.ToLower.EndsWith("bmp") _
Or x.ToLower.EndsWith("png")).ToArray
ListBox1.Items.AddRange(myimg)
ListBox1.SelectedIndex = 0
End If
End Using
这是您的问题可能开始的地方:
Private Sub img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles img.Click
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
myimg = IO.Directory.GetFiles(FolderBrowserDialog1.SelectedPath)
ListBox1.Items.AddRange(myimg)
ListBox1.SelectedIndex = 0
End If
End Sub
因此,在他们从您的ListBox
中进行选择之前:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.Refresh()
Me.BackgroundImage = Nothing
Me.BackgroundImage = Image.FromFile(ListBox1.SelectedItem.ToString)
End Sub
为什么不将ListBox
中显示的图像大小限制为5MB,例如:
Private Sub img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles img.Click
If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
myimg = IO.Directory.GetFiles(FolderBrowserDialog1.SelectedPath)
For Each imgfile As String In myimg
Dim allowedImgFile As New System.IO.FileInfo(imgfile)
If ((allowedImgFile.Length / 1024) <= 5) Then
ListBox1.Items.Add(imgfile)
End If
Next
ListBox1.SelectedIndex = 0
End If
End Sub
您需要通知用户,只有 5MB 或更小(或您喜欢的任何大小(的图像才会显示在列表中。