[英]Picturebox will not display image
我正在編寫一個程序,它將在運行時創建和加載圖片框。 問題是它們不會出現或顯示任何內容。 不知道我做錯了什么。 我已經檢查過,圖像的完整路徑是正確的。
Sub DrawScreen()
'Call g.DrawImage to draw whatever you want in here
Dim layers(Me.ListBox1.Items.Count) As PictureBox
For i = 0 To ListBox1.Items.Count - 1
'Create New Layer as picturebox
layers(i) = New PictureBox
layers(i).Parent = Me.picWatch
layers(i).BackColor = Color.Transparent
layers(i).Visible = True
Select Case ListBox1.Items(i)
Case "image"
'Debug.Print(ListofLayers(i).Full_Path)
layers(i).Image = Image.FromFile(ListofLayers(i).Full_Path)
layers(i).Top = ListofLayers(i).X
picWatch.Controls.Add(layers(i))
Case "shape"
'Dim g As Graphics
'g.DrawRectangle()
Case "text"
Dim g As Graphics = layers(i).CreateGraphics
g.DrawString(ListofLayers(i).Text, New Font("Arial", 12), Brushes.White, ListofLayers(i).X, ListofLayers(i).Y)
Case Else
Debug.Print(ListBox1.Items(i))
End Select
Next
Me.Refresh()
End Sub
您永遠不會將圖片框添加到表單中。 打電話給我Me.Controls.Add()
:
Me.Controls.Add(layers(i))
正如 LarsTech 已經指出的那樣:
你這里似乎有一個錯字:
layers(i).Top = ListofLayers(i).X
X
是控件水平位置的坐標,與.Left
相同。
然而, Y
是控件垂直位置的坐標,與.Top
相同。
使用CreateGraphics
是個壞主意。 首先,當控件被重繪時,你用它繪制的東西將被刪除。 而且由於你沒有處理它,你也會有內存泄漏。
為每個圖片框訂閱Paint
事件,然后在那里進行所有繪圖。
最后,請注意:VB.NET 中的數組聲明並沒有指定數組中有多少項,而是指定數組應該以什么索引結束。 由於數組是從零開始的,因此:
Dim layers(Me.ListBox1.Items.Count) As PictureBox
...等於這個:
Dim layers(0 To Me.ListBox1.Items.Count) As PictureBox
因此該數組將包含ListBox1.Items.Count
加一,因為括號內的內容僅指定下限和/或上限。
要創建具有“正確”數量的項目的數組,您應該始終指定大小減一:
Dim layers(Me.ListBox1.Items.Count - 1) As PictureBox
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.