【发布时间】:2014-01-11 16:18:09
【问题描述】:
我正在尝试编写一个函数,它应该创建一个指定大小的新位图并用指定的颜色填充它,问题是我在尝试将位图分配给任何属性时总是遇到异常或以其他几种方式使用它,例如从字典类型中获取位图以读取位图属性。
例如这个,不工作:
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 未处理 消息:未处理的异常 System.Drawing.dll 中出现“System.ArgumentException”类型 附加信息:无效参数。
这是函数,我缺少什么?
''' <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
位图是为了这个:
【问题讨论】:
-
您正在处理您尝试存储的同一个位图。
标签: .net vb.net graphics bitmap gdi+