我试图向ImageMagickNet类添加一个自定义函数。它应该使用ImageMagick.NET项目中的IsSimilarImage magick方法,但我对是否必须通过Magick++路由此方法感到困惑,因为在.NET端可用的任何功能都起源于Magick++。
我试图向ImageMagickNet类添加一个自定义函数。它应该使用ImageMagick.NET项目中的IsSimilarImage magick方法,但我对是否必须通过Magick++路由此方法感到困惑,因为在.NET端可用的任何功能都起源于Magick++。
这篇文章有些老,但因为没有得到回答,所以我来回答一下。
请注意,我没有查看ImageMagick库,所以下面代码中的任何实现细节都仅是一个示例。请用正确的实现替换垃圾代码。假设它正在导出有效的.NET对象,那么它将按照以下方式工作:
' 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