2015-04-01 72 views
0

我想填充3“小图像”到1大图像。图像已按顺序设置。 问题是,在这种情况下,该目录包含10个“小图像”。我怎样才能加载10张图片,将它们分成三组,然后保存我的“大图片”并继续显示下三张“小图片”?组目录文件成组VB.Net

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

避开值是零,我改变

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

+0

有你的问题太多虚假的细节。尽量减少你的问题,只是你需要的具体事情。例如,在这种情况下,你可以简单地说:“我有一个字符串列表,我怎样才能把这个列表分成三组?”字符串是文件路径的事实,并且您将在后面特别对它们做某些事情与手头上的主题无关。 – 2015-04-01 11:36:19

回答

1

您已经拥有列表(数组)中的文件,因此您无需将它们加载到ListBox控件中。你可以直接通过该数组循环。然而,而是采用了For Each循环,你可能希望通过3在每次迭代使用Integer迭代和增量迭代器,例如:

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 
+0

谢谢!我知道..我只是使用列表框作为临时视觉提示,其中实际上加载到数组的文件。最终产品将没有列表框。我会尝试你的方法现在 – db35m 2015-04-01 20:56:58

+0

我已经更新了我的代码,但现在如果在目录中有3个以上的文件,它会中断 – db35m 2015-04-01 21:25:18

+0

对不起,我认为你会看到我正在用那个简化的例子。我用更完整的例子更新了我的答案。 – 2015-04-02 00:46:15