我想寫一個函數,該函數應該創建一個指定大小的新位圖並用指定的顏色填充它,問題是我得到一個異常試圖將位圖分配給任何屬性或以其他幾種方式使用它,例如從字典類型獲取位圖以讀取位圖屬性。不能在這個函數中返回一個Bitmap對象
例如對於這一點,不工作:
Private Shadows Sub Shown() Handles MyBase.Shown
PictureBox1.BackgroundImage =
CreateSolidBitmap(New Size(16, 16), Color.Red)
End Sub
而且這工作直到我嘗試讀取位圖
Private Shadows Sub Shown() Handles MyBase.Shown
Dim dict As New Dictionary(Of Color, Bitmap) From
{
{Color.Red, CreateSolidBitmap(New Size(16, 16), Color.Red)}
}
' This throws the same exception above:
MsgBox(dict(Color.Red).Size.Width)
End Sub
例外說,這一刻:
System.ArgumentException未處理消息:在S中發生類型爲'System.ArgumentException'的未處理異常 ystem.Drawing.dll 附加信息:無效的參數。
這是函數,我錯過了什麼?
''' <summary>
''' Creates a bitmap filled with a solid color.
''' </summary>
''' <param name="FillColor">Color to fill the Bitmap.</param>
''' <returns>System.Drawing.Bitmap.</returns>
Private Function CreateSolidBitmap(ByVal Size As Size,
ByVal FillColor As Color) As Bitmap
' Create a bitmap.
Using bmp As New Bitmap(Size.Width, Size.Height)
' Create a graphics object.
Using g As Graphics = Graphics.FromImage(bmp)
' Create a brush using the specified color.
Using br As New SolidBrush(FillColor)
' Fill the graphics object with the brush.
g.FillRectangle(br, 0, 0, bmp.Width, bmp.Height)
End Using ' br
End Using ' g
Return bmp
End Using ' bmp
End Function
位圖是這個:
您正在處理您嘗試存儲的相同位圖。 – Plutonix