在这个应用程序中我正在处理用户从组合框中选择一个子文件名,并在该子文件夹中是(总是)5张图片。这些图像的名称是相机输出的图像。这个应用程序的目的是让用户在小缩略图框中预览5个图像,然后一次选择一个,并在按下"重命名"后,从radiobuttons选项重命名。按钮代码是将所选图像保存为所选名称。
我遇到的麻烦是我试图将这5张图片加载到名为picPreview1,2,3,4和5的5个图片框中,然后在选择(点击)图片框以显示更大的图片框时显示图片点击更大(这是将重命名的图片)。
目前我有这个代码,我上网了,但它错误地说Index was outside the bounds of the array.
我在网上找到的所有其他代码都是针对C#或者根本不起作用或者与此类似。我不确定如何修复此错误。任何帮助将不胜感激。
代码:
Public Sub Picture_Preview()
Dim FileName As String = cmbPartNumber.Text
Dim pics() As PictureBox = {picPreview1, picPreview2, picPreview3, picPreview4, picPreview5}
Dim List() As String = Directory.GetFiles("\\HOMESHARE01\Public\Reference Cards\Completed Reference Cards by Part Number" & "\" & FileName, "*.jpg")
For i As Integer = 0 To pics.Count - 1
pics(i).Image = Image.FromFile(List(i + 5))
Next
End Sub
完整代码:
Imports System.IO
Public Class Form1
Dim Pictype As String
Private HighlightedPictureBox As PictureBox = Nothing
Private Sub cmbPartNumber_TextChanged(sender As Object, e As EventArgs) Handles cmbPartNumber.TextChanged
Dim FileName As String = cmbPartNumber.Text
lblRenameAs.Text = Pictype & FileName
Label1.Visible = True
picPreview1.Visible = True
picPreview2.Visible = True
picPreview3.Visible = True
picPreview4.Visible = True
picPreview5.Visible = True
Picture_Preview()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim Folder = From dir In IO.Directory.GetDirectories("\\HOMESHARE01\Public\Reference Cards\Completed Reference Cards by Part Number")
Select IO.Path.GetFileName(dir)
cmbPartNumber.Items.AddRange(Folder.ToArray)
End Sub
Private Sub btnSavePic_Click(sender As Object, e As EventArgs) Handles btnSavePic.Click
'Check if no radio button checked
If Not (radSpindle.Checked = True Or radRotorTop.Checked = True Or radRotorBottom.Checked = True Or radDunnageFinal.Checked = True Or radDunnageLayer.Checked = True) Then
MsgBox("Please Select Picture Type Before Renaming")
Exit Sub
End If
'Check which radio button is checked
If radSpindle.Checked = True Then
Pictype = "PicSpindle"
End If
If radRotorTop.Checked = True Then
Pictype = "PicRotorTop"
End If
If radRotorBottom.Checked = True Then
Pictype = "PicRotorBottom"
End If
If radDunnageFinal.Checked = True Then
Pictype = "PicDunnageFinal"
End If
If radDunnageLayer.Checked = True Then
Pictype = "PicDunnageLayer"
End If
End Sub
Public Sub Picture_Preview()
Dim FileName As String = cmbPartNumber.Text
Dim pics() As PictureBox = {picPreview1, picPreview2, picPreview3, picPreview4, picPreview5}
Dim List() As String = Directory.GetFiles("\\HOMESHARE01\Public\Reference Cards\Completed Reference Cards by Part Number" & "\" & FileName, "*.jpg")
For i As Integer = 0 To pics.Count - 1
pics(i).Image = Image.FromFile(List(i + 5))
Next
End Sub
Private Sub picPreview1_Click(sender As Object, e As EventArgs) Handles picPreview1.Click
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
End Sub
Private Sub picPreview2_Click(sender As Object, e As EventArgs) Handles picPreview2.Click
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
End Sub
Private Sub picPreview3_Click(sender As Object, e As EventArgs) Handles picPreview3.Click
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
End Sub
Private Sub picPreview4_Click(sender As Object, e As EventArgs) Handles picPreview4.Click
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
End Sub
Private Sub picPreview5_Click(sender As Object, e As EventArgs) Handles picPreview5.Click
'Get the rectangle of the control and inflate it to represent the border area
Dim BorderBounds As Rectangle = DirectCast(sender, PictureBox).ClientRectangle
BorderBounds.Inflate(-1, -1)
'Use ControlPaint to draw the border.
'Change the Color.Red parameter to your own colour below.
ControlPaint.DrawBorder(DirectCast(sender, PictureBox).CreateGraphics,
BorderBounds,
Color.Red,
ButtonBorderStyle.Solid)
If Not (HighlightedPictureBox Is Nothing) Then
'Remove the border of the last PictureBox
HighlightedPictureBox.Invalidate()
End If
'Rememeber the last highlighted PictureBox
HighlightedPictureBox = CType(sender, PictureBox)
End Sub
Private Sub radSpindle_Click(sender As Object, e As EventArgs) Handles radSpindle.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicSpindle"
lblRenameAs.Text = Pictype & FileName
End Sub
Private Sub radRotorTop_Click(sender As Object, e As EventArgs) Handles radRotorTop.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicRotorTop"
lblRenameAs.Text = Pictype & FileName
End Sub
Private Sub radRotorBottom_Click(sender As Object, e As EventArgs) Handles radRotorBottom.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicRotorBottom"
lblRenameAs.Text = Pictype & FileName
End Sub
Private Sub radDunnageLayer_Click(sender As Object, e As EventArgs) Handles radDunnageLayer.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicDunnageLayer"
lblRenameAs.Text = Pictype & FileName
End Sub
Private Sub radDunnageFinal_Click(sender As Object, e As EventArgs) Handles radDunnageFinal.Click
Dim FileName As String = cmbPartNumber.Text
Pictype = "PicDunnageFinal"
lblRenameAs.Text = Pictype & FileName
End Sub
End Class
答案 0 :(得分:0)
解决了......
旧代码:
For i As Integer = 0 To pics.Count - 1
pics(i).Image = Image.FromFile(List(i + 5))
Next
修正:
For i As Integer = 0 To pics.Count - 1
pics(i).Image = Image.FromFile(List(i))
Next