10.3   Adding HALCON/.NET to an Application

10.3.1   Adding HALCON/.NET to a .NET Core Application

To leverage the cross-platform nature of .NET Core, the interface is offered in two package variants. Both of them contain the complete HALCON/.NET and HDevEngine/.NET language bindings.

MVTec.HalconDotNet

This package targets .NET Standard 2.0 and can be used on all architectures supported by HALCON.

MVTec.HalconDotNet-Windows

This package targets .NET Core 3.1 and additionally includes Windows Forms and WPF controls for integrating HALCON windows into GUI applications. This package can only be used on Windows since other .NET Core 3.1 implementations do not offer Windows Forms or WPF support.

Use the following command to add package references to your project:


dotnet add package <PACKAGE_NAME> -v 21050

While NuGet uses SemVer to version packages, HALCON versions are not SemVer compatible. To avoid unnecessary conflicts, .NET Core packages receive a SemVer-compatible version by combining the major, minor, and revision into a new major, e.g.:

HALCON/.NET is not a self-contained package but an interface to the native HALCON library. Therefore, the package version used by your project has to match the installed HALCON version exactly.

Even for maintenance releases, which are backwards compatible at application level, mixing of binaries from different releases is not recommended because the internal communication between HALCON/.NET and the native HALCON library is not guaranteed to be always compatible. Therefore, the revision of the HALCON release is also part of the SemVer major version. The SemVer minor and patch version are reserved for fully backwards-compatible intermediate releases of the package itself. See section “.NET Core” for updating dependencies.

To use the packages offline, download them from the NuGet website. Consider adding a nuget.config file to your project. This helps to source it reliably from a file system.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
     <add key="LocalHalconPackages" value="path/to/package" />
  </packageSources>
</configuration>

When developing an application with HALCON XL, select the following XL packages:

For upgrading HALCON/.NET in relation to a HALCON upgrade, see section “.NET Core”.

10.3.2   Adding HALCON/.NET to a .NET Framework Application

You add HALCON/.NET to an application with the following steps:

For the first application, customize Visual Studio's toolbox (see section “Customizing Visual Studio's Toolbox”).

For each application, add a reference to HALCON/.NET (section “Adding a Reference to HALCON/.NET”) and specify the namespace (section “Specifying the Namespace”).

10.3.2.1   Customizing Visual Studio's Toolbox

The HALCON/.NET assembly provides not only a class library but also one control: HSmartWindowControl (or HSmartWindowControlWPF), which contains a HALCON graphics window for visualizing images and results.

The older control HWindowControl is still available for backwards compatibility but it is strongly advised against using this control for new projects (see section “Window Controls for Visualization” for information about the differences).

You can add these controls to Visual Studio's toolbox by performing the following steps. Note that the exact menu names slightly differ in different versions of Visual Studio:

  1. Right-click the toolbox and select Choose Items (Customize Toolbox in previous versions of Visual Studio). This will open a dialog displaying all available .NET Framework components in a tab.
  2. Click Browse, navigate to the directory %HALCONROOT%\bin\dotnet20 (Visual Studio 2005) or %HALCONROOT%\bin\dotnet35 (Visual Studio 2008 or higher) and select HALCON/.NET DLL.
  3. The icon of HSmartWindowControl and the older HWindowControl appear in the toolbox.

HALCON XL applications: When developing an application with HALCON XL, select HALCON/.NET XL DLL instead of HALCON/.NET DLL. In the toolbox, the control appears with the same name but with a different icon. You can add both HALCON versions to the toolbox but only one of them to an application.

10.3.2.2   Adding a Reference to HALCON/.NET

In many applications, you will use at least one instance of HSmartWindowControl to visualize results. By adding the control to the form (as described in section “Adding and Customizing HSmartWindowControl for the Visualization”), you automatically create a reference to the assembly HALCON/.NET DLL.

If you do not want to use HSmartWindowControl, add a reference as follows:

  1. Right-click References in the Solution Explorer and select Add Reference.
  2. Click Browse, navigate to the subdirectory %HALCONROOT%\bin\dotnet20 (Visual Studio 2005) or %HALCONROOT%\bin\dotnet35 (Visual Studio 2008) and select the assembly HALCON/.NET DLL.

HALCON XL applications: When developing an application with HALCON XL, select HALCON/.NET XL DLL instead of HALCON/.NET DLL. If you already added a reference to the HALCON version, simply delete this reference and add one to HALCON/.NET XL DLL.