将目录文件分组 VB.Net

group directory files into sets VB.Net

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

更新: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 整除的图像数量,我会得到一个空错误 我如何处理文件为空基本上允许它们为空?

更新 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 为空?

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

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