这可能吗?我想将自定义颜色作为参数传递,我想要接收图像(例如矩形)。
Public Function createIcon(ByVal c As Color) As Bitmap
Dim g As Graphics
Dim Brush As New SolidBrush(c)
g.FillRectangle(Brush, New Rectangle(0, 0, 20, 20))
Dim bmp As New Bitmap(20, 20, g)
Return bmp
End Function
我试过这种方式并没有成功。
答案 0 :(得分:1)
Bitmap
:包含图片的画布(在内存中)。Graphics
:允许您在关联的画布上绘图的工具集。考虑到这一点,这是解决方案:
Public Function CreateIcon(ByVal c As Color, ByVal x As Integer, ByVal y As Integer) As Bitmap
Dim icon As New Bitmap(x, y)
Using g = Graphics.FromImage(icon)
Using b As New SolidBrush(c)
g.FillRectangle(b, New Rectangle(0, 0, 20, 20))
End Using
End Using
Return icon
End Function
这里的Using
块仅用于正确处理图形资源(通过在块结束时自动调用它们的Dispose
方法)。您需要来执行此操作,否则您将泄露图形资源。
答案 1 :(得分:0)
好的,明白了。我将分享我的所作所为以防万一。
Public Function createIcon(ByVal c As Color, ByVal x As Integer, ByVal y As Integer) As Bitmap
createIcon = New Bitmap(x, y)
For i = 0 To x - 1
For j = 0 To y - 1
If i = 0 Or j = 0 Or i = x - 1 Or j = y - 1 Then
createIcon.SetPixel(i, j, Color.Black)
Else
createIcon.SetPixel(i, j, c)
End If
Next
Next
Return createIcon
End Function
此功能将为您提供带黑色边框的彩色矩形。