通过扩展图像 属性 创建自定义 ImageList class
Creating a custom ImageList class by extended image property
我想用自定义图像列表替换我现有的图像列表。
Public Class clsImageList
Public Images As New List(Of clsImageItem)
Public Sub Add(ByVal uGUID As String, ByRef uImage As Image)
Dim nItem As New clsImageItem(uImage, uGUID)
Images.Add(nItem)
End Sub
Public Function Image(ByVal uIndex As Integer) As Image
Return Images.Item(uIndex).Image
End Function
End Class
Public Class clsImageItem
Public ReadOnly Property [Image] As Image
Private _sGUID As String
Public Sub New(uImage As Image, uGUID As String)
Image = uImage
_sGUID = uGUID
End Sub
End Class
我正在尝试使我的 class 与 .NET ImageList 兼容,这样我就不必更改太多代码。
Using g As Graphics = Graphics.FromImage(SomeBitmap)
g.DrawImage(il.Images(myIndex), 0, 0, 256, 256)
End Using
我得到的错误是 "There are no overloads for this function."。
我理解这个错误,它无法呈现我的 clsImageItem,但我想知道我是否可以扩展图形以接受我的 clsImageItem。
我必须更换
.il.Images(myIndex)
来自
.il.Images(myIndex).Image
或者有更简单/更方便的方法吗?
您可以使用 il.Images(myIndex).Image
引用 il
中的 Images
列表。
您可以使用 il.Image(myIndex)
引用 il
中的 Image
函数。
I wonder if I could perhaps extend the Graphics to accept my clsImageItem?
是的,您可以创建一个扩展方法来接受您的 clsImageItem
class。
Imports System.Drawing
Imports System.Runtime.CompilerServices
Module GraphicsExtensions
<Extension()>
Public Sub DrawImage(ByVal g As Graphics, c As clsImageItem, x As Integer, y As Integer , w As Integer, h As integer)
g.DrawImage(c.Image, x, y, w, h)
End Sub
End Module
然后要使用这个重载,导入包含扩展方法的模块然后你可以这样写:
g.DrawImage(il.Images(myIndex), 0, 0, 256, 256)
有关扩展方法的更多信息
Extension methods enable developers to add custom functionality to
data types that are already defined without creating a new derived
type. Extension methods make it possible to write a method that can be
called as if it were an instance method of the existing type.
我想用自定义图像列表替换我现有的图像列表。
Public Class clsImageList
Public Images As New List(Of clsImageItem)
Public Sub Add(ByVal uGUID As String, ByRef uImage As Image)
Dim nItem As New clsImageItem(uImage, uGUID)
Images.Add(nItem)
End Sub
Public Function Image(ByVal uIndex As Integer) As Image
Return Images.Item(uIndex).Image
End Function
End Class
Public Class clsImageItem
Public ReadOnly Property [Image] As Image
Private _sGUID As String
Public Sub New(uImage As Image, uGUID As String)
Image = uImage
_sGUID = uGUID
End Sub
End Class
我正在尝试使我的 class 与 .NET ImageList 兼容,这样我就不必更改太多代码。
Using g As Graphics = Graphics.FromImage(SomeBitmap)
g.DrawImage(il.Images(myIndex), 0, 0, 256, 256)
End Using
我得到的错误是 "There are no overloads for this function."。
我理解这个错误,它无法呈现我的 clsImageItem,但我想知道我是否可以扩展图形以接受我的 clsImageItem。
我必须更换
.il.Images(myIndex)
来自
.il.Images(myIndex).Image
或者有更简单/更方便的方法吗?
您可以使用 il.Images(myIndex).Image
引用 il
中的 Images
列表。
您可以使用 il.Image(myIndex)
引用 il
中的 Image
函数。
I wonder if I could perhaps extend the Graphics to accept my clsImageItem?
是的,您可以创建一个扩展方法来接受您的 clsImageItem
class。
Imports System.Drawing
Imports System.Runtime.CompilerServices
Module GraphicsExtensions
<Extension()>
Public Sub DrawImage(ByVal g As Graphics, c As clsImageItem, x As Integer, y As Integer , w As Integer, h As integer)
g.DrawImage(c.Image, x, y, w, h)
End Sub
End Module
然后要使用这个重载,导入包含扩展方法的模块然后你可以这样写:
g.DrawImage(il.Images(myIndex), 0, 0, 256, 256)
有关扩展方法的更多信息
Extension methods enable developers to add custom functionality to data types that are already defined without creating a new derived type. Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.