Run Halcon in Visual Studio (C# configures Halcon environment)
Foreword:
This article records the various pitfalls of running Halcon in VS, and builds a VS halcon project from scratch, posting for experience.
1. Create a VS project and a Halcon project
VS select Windows Forms application (.NET Framework), as shown in the figure below:
Just write the most common image reading code in Halcon, as follows:
Then convert the halcon file into a C# file. Click the file in the upper left corner, select the export language, and configure it as follows:
2. Configuration environment
In the Halcon folder, find the bin folder, as follows:
Select dotnet35 to open, and then copy halcondotnet.dll, as follows:
In the VS project, select "WindowsFormsApp1—bin—Debug" in turn to open the configuration of the VS project folder. Copy halcondotnet.dll to this directory. As follows:
There are three pitfalls here:
1. The file starting with windows in the above picture is only available after running VS, don’t worry;
2. Some tutorials will also copy halcon.dll here, but halcon The .dll is not compiled by C#, so the C# environment cannot open the file, and an error will be reported, just don’t quote the file;
3. The license file should also be copied there, but in the folder of our local halcon, the license folder There is only one txt file in it, which cannot be used normally. You need to download the pass file and put it in this folder (the pass file is updated every month, pay attention to the date of download).
3. Prepare VS
First, click Reference→Add Reference to add halcondotnet.dll. As follows:
Note : Do not reference halcon.dll, otherwise an error will be reported: Please ensure that this file is accessible and is a valid assembly or COM component. As follows:
After that, click the toolbox, right-click "All Windows Forms", and click Select Items.
Then click "Browse" to reference halcondotnet.dll:
Then find HWidowControl
and place it in the form, and then place a button, as follows:
4. Run
Open the cs file generated by halcon, find the action function, and copy it (currently, this function is useful in the entire file)
button in the Form1 window, enter the code interface,
paste the code in the action function here, and Replace the image path with your own image. As follows:
Then start and run, but after clicking the button, there is still no picture. This is because we did not add the display code, as follows:
After adding, start and run again, click the button, and the picture is displayed.
over