将目录文件分组成VB.Net

时间:2015-04-01 10:05:51

标签: vb.net arraylist set

我正在尝试填充3"小图像"成1大图。图像已按顺序设置。 问题是在这种情况下,目录包含10"小图像"。如何加载10张图片,将它们分成三组,保存我的"大图像"并继续下三个"小图像"?

更新:2

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim files As String() = Directory.GetFiles("C:\pics\")

        For i As Integer = 0 To files.Length - 1 Step 3
            Dim file1 As String = Nothing
            Dim file2 As String = Nothing
            Dim file3 As String = Nothing
            file1 = files(i)
            If i < files.Length - 1 Then
                file2 = files(i + 1)
            End If
            If i < files.Length - 2 Then
                file3 = files(i + 2)
            End If

            'Here the background is created and filled.
            Dim img As New Bitmap(2400, 3000)
            Dim img_back As Graphics = Graphics.FromImage(img)
            img.SetResolution(300, 300)
            img_back.FillRectangle(Brushes.White, 0, 0, 2400, 3000)
            ' Sets Spot names for ech ticket
            Dim Ticket_1 As New Bitmap(file1, True)
            Dim Ticket_2 As New Bitmap(file2, True)
            Dim Ticket_3 As New Bitmap(file3, True)

            'This creates New merged image
            Dim g As Graphics = Graphics.FromImage(img)
            g.DrawImage(Ticket_1, 500, 200)
            g.DrawImage(Ticket_2, 500, 1000)
            g.DrawImage(Ticket_3, 500, 1800)

            'We Save rendered image and display on picturebox
            img.Save("C:\pics\list\" & i & "NewTicket List.jpg")
            PictureBox1.Image = img
        Next
        'PictureBox1.Image.Save("C:\pics\New" + 1)
        MsgBox("All Done!")

    End Sub

如果数组包含可被3整除的非图像数量,则会出现空错误 我如何处理文件为null基本上允许它们为空?

更新3

绕过值为null我改变了

 Dim files As String() = Directory.GetFiles("C:\pics\")

        For i As Integer = 0 To files.Length - 1 Step 3
            Dim file1 As String = Nothing
            Dim file2 As String = Nothing
            Dim file3 As String = Nothing
            file1 = files(i)
            If i < files.Length - 1 Then
                file2 = files(i + 1)
            End If
            If i < files.Length - 2 Then
                file3 = files(i + 2)
            End If

到这个

 Dim files As String() = Directory.GetFiles("C:\pics\")

        For i As Integer = 0 To files.Length - 1 Step 3
            Dim file1 As String = "C:\pics\NoImage\NoImage.jpg"
            Dim file2 As String = "C:\pics\NoImage\NoImage.jpg"
            Dim file3 As String = "C:\pics\NoImage\NoImage.jpg"
            file1 = files(i)
            If i < files.Length - 1 Then
                file2 = files(i + 1)
            End If
            If i < files.Length - 2 Then
                file3 = files(i + 2)
            End If

我如何允许Ticket_1为空?

1 个答案:

答案 0 :(得分:1)

您已经将文件放在列表(数组)中,因此您无需将它们加载到ListBox控件中。您可以直接循环遍历该数组。但是,您可能希望使用For Each迭代器而不是使用Integer循环,并在每次迭代时将迭代器增加3,例如:

For i As Integer = 0 To files.Length - 1 Step 3
    Dim img As New Bitmap(2400, 3000)
    Dim img_back As Graphics = Graphics.FromImage(img)
    img.SetResolution(300, 300)
    img_back.FillRectangle(Brushes.White, 0, 0, 2400, 3000)
    Dim g As Graphics = Graphics.FromImage(img)

    Dim Ticket_1 As New Bitmap(files(i), True)
    g.DrawImage(Ticket_1, 500, 200)

    If i < files.Length - 1 Then
        Dim Ticket_2 As New Bitmap(files(i + 1), True)
        g.DrawImage(Ticket_2, 500, 1000)
    End If

    If i < files.Length - 2 Then
        Dim Ticket_3 As New Bitmap(files(i + 2), True)
        g.DrawImage(Ticket_3, 500, 1800)
    End If

    ' Use img...
Next