简体   繁体   English

将目录文件分组为VB.Net

[英]group directory files into sets VB.Net

I am trying to populate 3 "Small images" into 1 large image. 我正在尝试将3个“小图像”填充为1个大图像。 The images have been set in order. 图像已按顺序设置。 The problem is that in this case, the directory contains 10 "Small images". 问题在于,在这种情况下,目录包含10个“小图像”。 how can I load 10 images, separate them into groups of three, save my "large Image" and continue with the next three "small images"? 如何加载10张图像,将它们分成三个组,保存“大图像”,然后继续下三个“小图像”?

UPDATE: 2 更新: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

if array contains non amount of images divisible by 3 i get a null error How can i deal with the files being null Basically allowing them to be null? 如果数组中包含的图像数量不能被3整除,我会得到null错误我该如何处理文件为null基本上允许它们为null?

UPDATE 3 更新3

to get around the value being null i Changed 避开值为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

to this 对此

 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

how can i allow the Ticket_1 to be null? 我如何允许Ticket_1为空?

You already have the files in a list (an array), so you don't need to load them into a ListBox control. 您已经在列表(数组)中保存了文件,因此不需要将它们加载到ListBox控件中。 You can just loop directly through that array. 您可以直接在该数组中循环。 However, instead of using a For Each loop, you may want use an Integer iterator and increment the iterator by 3 at each iteration, for instance: 但是,您可能希望使用Integer迭代器,而不是使用For Each循环,并在每次迭代时将迭代器增加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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM