Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending ImageMagickNet

I’m trying to add a custom function to class ImageMagickNet. It should use the IsSimilarImage magick method from the ImageMagick.NET project but I’m confused as to whether I have to route this method through the Magick++, as any functionality available to the .NET side originates in the Magick++.

like image 616
Becky Green Avatar asked Feb 09 '10 20:02

Becky Green


1 Answers

This is pretty old but as is it unanswered, here goes.

Please note that I have not looked at the ImageMagick libraries, so any implementation details in the below code is strictly an example. Replace rubbish with correct implementation. Assuming it is exporting valid .NET objects, this is how it would work:

' Put your extension methods or properties in a clearly labeled module file, on its own within your project
Module ImageMagickNetExtensions

    ' Define an extension method by using the ExtensionAttribute, and make the first argument
    ' for the method the type that you wish to extend. This will serve as a reference to the extended
    ' instance, so that you can reference other methods and properties within your extension code.
    <Extension()> _
    Public Function SomeExtensionFunction(ByVal imn As ImageMagickNet, ByVal filename As String) As Boolean
        Return imn.IsSimilarImage(filename)
    End Function

End Module

Class SomeClass
    ' To use your extension method within your project containing the extension module, simply
    ' call it on any valid instance of the type you have extended. The compiler will call your code
    ' whenever it sees reference to it, passing a reference to your extended instance.
    Private imn As New ImageMagickNet

    Private Sub DoSomething()
        If imn.SomeExtensionFunction("c:\someimage.jpg") Then
            ...
        End If
    End Sub
End Class
like image 146
Drunken Code Monkey Avatar answered Oct 26 '22 00:10

Drunken Code Monkey