我在YouTube上找到了一个代码,当我在visual basic上使用它并调试它并查找图片时,但是当我想保存它们时,软件会给我这条消息here。
Private Sub btnSaveImages_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnSaveImages.Click
Dim dir_name As String = txtDirectory.Text
If Not dir_name.EndsWith("\") Then dir_name &= "\"
For Each pic As PictureBox In flpPictures.Controls
Dim bm As Bitmap = pic.Image
Dim filename As String = pic.Tag
filename = _
filename.Substring(filename.LastIndexOf("/") + _
1)
Dim ext As String = _
filename.Substring(filename.LastIndexOf("."))
Dim full_name As String = dir_name & filename
Select Case ext
Case ".bmp"
bm.Save(full_name, Imaging.ImageFormat.Bmp)
Case ".gif"
bm.Save(full_name, Imaging.ImageFormat.Gif)
Case ".jpg", "jpeg"
bm.Save(full_name, Imaging.ImageFormat.Jpeg)
Case ".png"
bm.Save(full_name, Imaging.ImageFormat.Png)
Case ".tiff"
bm.Save(full_name, Imaging.ImageFormat.Tiff)
Case Else
MessageBox.Show( _
"Unknown file type " & ext & _
" in file " & filename, _
"Unknown File Type", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
End Select
Next pic
Beep()
End Sub
答案 0 :(得分:1)
问题在于LastIndexOf没有找到。在你的文件名中。然后将-1传递给SubString,抛出您正在看到的错误。
不使用自己编写文件解析,而是使用System.Io.Path的方法,例如
System.IO.Path.GetDirectoryName(filename)
System.IO.Path.GetFileName(filename)
System.IO.Path.GetFileNameWithoutExtension(filename)
Private Sub btnSaveImages_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnSaveImages.Click
For Each pic As PictureBox In flpPictures.Controls
Dim bm As Bitmap = pic.Image
Dim path As String = pic.Tag
Dim filename = IO.Path.GetFileName(path)
Dim ext = IO.Path.GetExtension(path)
Dim full_name = IO.Path.Combine(txtDirectory.txt, filename)
...