从文件中获取图像并将其添加到阵列

时间:2012-01-12 21:30:19

标签: vb.net visual-studio-2010

我正在尝试创建一个程序来检查是否有人要去欢乐时光。如果没有,它会列出那些没有的人,并将他们的照片放在他们的名字旁边。

我能够实现所有功能,但是在本地获取图像并将它们存储在一个数组中(将添加到pictureArray(i)中)。

(你可以看到注释掉的部分是我试图获取图像的地方......)

有什么想法吗?

Public Class Form1
Dim ITLPList() As String = {"Name 1", "Name 2", "Name 3", "Name 4", "Name 5", "Name 6"}
'  Dim imageList As New ImageList
' Dim fileSteam As New System.IO.FileStream(sFileName, System.IO.FileMode.Open)
' Dim img As Image
' Dim sFileName As String = "C:\Users\turcotd\Desktop\ITLPers\itlp1.jpg"

Dim itlpTally() As String
Dim labelArray(5) As Label
Dim pictureArray(5) As PictureBox

Dim intTally As Integer
Dim i As Integer = 0

Public itlpIndex As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    itlpName.Text = ITLPList(0)
    labelArray(0) = lblPerson0
    labelArray(1) = lblPerson1
    labelArray(2) = lblPerson2
    labelArray(3) = lblPerson3
    labelArray(4) = lblPerson4

    pictureArray(0) = picITLP0
    pictureArray(1) = picITLP1
    pictureArray(2) = picITLP2
    pictureArray(3) = picITLP3
    pictureArray(4) = picITLP4

End Sub

Private Sub btnYes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYes.Click

    If (i < 6) Then
        itlpName.Text = ITLPList(i)
        i = i + 1

    End If
End Sub

Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.Click

    If (i < 6) Then
        'Names
        itlpName.Text = ITLPList(i)
        intTally = intTally + 1
        lblTally.Text = intTally
        labelArray(i).Text = ITLPList(i)

        'Images
        '    img = Image.FromStream(fileSteam)
        '   fileSteam.Close()
        '  imageList.Images.Add(img)
        '  pictureArray(i).Image = imageList.Images.Item(0)
        '  img.Dispose()

        ' img = Image.FromFile(sFileName)

        i = i + 1
    End If
    itlpName.Text = ITLPList(i)
End Sub
End Class

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式将目录中的所有jpg作为FileInfo

Dim dir = New IO.DirectoryInfo("C:\Users\turcotd\Desktop\ITLPers")
Dim images = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories).ToList

答案 1 :(得分:0)

从文件中将文件加载到PictureBox中是一项非常简单的操作:

picITLP0.Image = Image.FromFile("C:\Users\turcotd\Desktop\ITLPers\itlp1.jpg")