我被困住了,不知道该怎么办
我正在尝试制作image viewer
,并将image
个文件夹的所有路径放在listbox
中,
然后,当我尝试在index 2
列表中选择时,我收到错误并且说“OutOfMemoryException”
这是我的代码
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
,请帮忙 谢谢:))
答案 0 :(得分:2)
您可能在列表中没有有效的图像文件。寻找例外:
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
答案 1 :(得分:1)
这是你的问题可能开始的地方:
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或更小的图像(或您喜欢的任何尺寸)才会显示在列表中。