2013-02-19 63 views
0

可能有一個簡單的答案,但我已經嘗試了很多事情,似乎沒有任何工作,除非我只是手動顯示圖像,但我想要的是對其他圖片使用這個子圖,以後我會修復它以顯示這些圖像。我的代碼如下如何顯示部分使用圖片框陣列的for循環

Private Sub updateStandardToolbar() 
    'Draws the sprites to the pictureboxes 
    Dim bit As New Bitmap(gridSize, gridSize) 
    Dim dest_rect As Rectangle = New Rectangle(0, 0, gridSize, gridSize) 
    Dim i As Integer = 0 

    ' Associate a Graphics object with the Bitmap 
    Dim gr As Graphics = Graphics.FromImage(bit) 

    For y As Integer = 0 To standardNum.Y 
     For x As Integer = 0 To standardNum.X 
      Call getSrcRect(x, y) 

      'Copy that part of the image. 
      gr.DrawImage(bmpStandardTile, dest_rect, src_rect, GraphicsUnit.Pixel) 

      'Display the result. 
      pb(i).Image = bit 

      i += 1 
     Next 
    Next 
End Sub 

這段代碼的問題是所有的pictureboxes(PB())顯示同樣的畫面是我的形象上的最後一個。 StandardNum.x和standardNum.y分別是保存3和1的變量。 bmpStandardTile是我想要複製的圖像,希望dest_rect和src_rect可以自我解釋=)。任何幫助表示讚賞。

回答

0

哦。在查看爲什麼圖片框顯示相同的圖像後,我只找到了自己問題的答案。這裏是我的代碼,如果其他人有類似或相同的問題,我如何修復它。

Private Sub updateStandardToolbar() 
    'Draws the sprites to the pictureboxes 
    Dim bit As Bitmap 
    Dim dest_rect As Rectangle = New Rectangle(0, 0, gridSize, gridSize) 
    Dim i As Integer = 0 

    ' Associate a Graphics object with the Bitmap 
    'Dim gr As Graphics 

    'For u As Integer = 0 To maxPics 
    ' bit = New Bitmap(gridSize, gridSize) 
    'Next 

    For y As Integer = 0 To standardNum.Y 
     For x As Integer = 0 To standardNum.X 
      bit = New Bitmap(gridSize, gridSize) 
      Dim gr As Graphics = Graphics.FromImage(bit) 
      Call getSrcRect(x, y) 

      'Copy that part of the image. 
      gr.DrawImage(bmpStandardTile, dest_rect, src_rect, GraphicsUnit.Pixel) 

      'Display the result. 
      pb(i).Image = bit 

      i += 1 
     Next 
    Next 
End Sub 

答案其實很簡單。傻我。我忘記清除位圖圖像,因此它不會更改最後一個圖片盒正在使用的圖片。

bit = New Bitmap(gridSize, gridSize) 

從我的錯誤無論如何據悉=)