I would like to replace my existing ImageLists with custom imagelists.
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
I am trying to make my class compatible with the .NET ImageList so that I don't have to change much code.
Using g As Graphics = Graphics.FromImage(SomeBitmap)
g.DrawImage(il.Images(myIndex), 0, 0, 256, 256)
End Using
The error I'm getting is "There are no overloads for this function.".
I do understand this error, it can't render my clsImageItem, but I wonder if I could perhaps extend the Graphics to accept my clsImageItem.
Do I have to replace
.il.Images(myIndex)
by
.il.Images(myIndex).Image
or is there a simpler / more convinient way?
You can use il.Images(myIndex).Image
that refers to the Images
list in il
.
You can use il.Image(myIndex)
that refers to the Image
function in il
.
I wonder if I could perhaps extend the Graphics to accept my clsImageItem?
Yes you can create an extension method that accepts your 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
Then to use this overload, import the module that holds the extension method and then you can write:
g.DrawImage(il.Images(myIndex), 0, 0, 256, 256)
More information about Extension Methods
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.