C# (Windows)

You are currently viewing the documentation for the latest version (3.0.0). To access a different version, click the "Switch version" button located in the upper-right corner of the page.

■ If you are not sure which version of the product you are currently using, please feel free to contact Mech-Mind Technical Support.

This topic introduces how to use the C# samples of Mech-DLK SDK on Windows. For sample descriptions, refer to Sample Usage Guide.

Installation Package Contents

The files related to C# samples in the Mech-DLK SDK installation package are as follows:

Contents Description

csharp/docs

C# interface documentation

csharp/examples

C# example programs

csharp/lib

C# SDK dynamic libraries (mmind_dl_sdk_csharp.dll file)

  • Before you compile and run C# samples, copy the relevant files in the cpp/bin and cpp/resources directories to a specified location. Refer to Compile and Run Samples for detailed instructions.

  • If the file or directory name used to run or compile the sample contains Chinese, find the Language and Region option in Windows Settings, open Beta: Use Unicode UTF-8 for worldwide language support, and then restart your computer.

Prerequisites

To use the C# samples in Mech-DLK SDK, the following software must be installed:

Install Mech-DLK SDK

Obtain the latest version of Mech-DLK SDK from Installation Guide and complete installation.

Install Visual Studio

  1. Download the Visual Studio installation package.

  2. During installation, select Desktop & Mobile under .NET Desktop development workloads. If the .NET SDK component is displayed in the installation interface, it is recommended to select it together, and then click Install in the lower-right corner.

    sample csharp workload
  • Install Visual Studio (Version 2019 or Above).

  • The C# example projects in the current installation package are based on .NET Framework 4.6.1. It is recommended to use an x64 configuration when compiling and running the samples.

Install OpenCV (Optional)

If you need to use OpenCV, download and install the C# version of OpenCV, i.e. OpenCvSharp.

This sample uses OpenCV 4.12.0 as an example. If you are using other versions, adjust the configuration according to the actual situation.

Install HALCON (Optional)

If you need to use the HALCON function, download and install the C# version of HALCON.

Example Adjustment Instructions

Before you compile and run various C# samples, you can adjust the input images, model packages, and related resource files according to your actual business needs. The following contents provide examples of common adjustment items.

  • Input image: Replace the image to be detected, or modify the image reading path according to actual requirements.

  • Model package: A defect segmentation model package is provided by default in the installation package. You can replace it with the one you need according to your actual business needs.

  • Output: Adjust how detection results are output based on project requirements, including modifying the save path, file name, or output location of result images.

  • Resource files: You can add or replace images, model packages, and related resource files required by the business in the resources directory.

Compile and Run Samples

After completing relevant adjustments, you can follow these steps to compile and run the sample.

Basic Sample

Compile the Sample

You can follow these steps to compile Basic samples:

  1. Copy the csharp/lib directory and paste the mmind_dl_sdk_csharp.dll file to the directory where the basic.sln solution file of the Basic sample is stored (that is, csharp\examples\basic).

  2. Double-click the basic.sln file and open it in Visual Studio.

  3. On the toolbar of Visual Studio, set Solution Configuration to Release and Solution Platform to x64. In the Visual Studio menu bar, select Build  Build Solution.

    vs setting
  4. After the compilation, you can see the generated executable file in the csharp\examples\basic\build directory. The example_csharp_basic.exe is used as an example in this section.

Run the Sample

  1. Copy the hasp_rt.exe file and all .dll files in the cpp/bin directory to the directory where the generated example_csharp_basic.exe file is stored.

    copy to csharp basic directory
  2. Copy the resources folder in the cpp folder to the directory where the basic.sln file is stored.

    copy to csharp basic sln directory
  3. Double-click the generated example_csharp_basic.exe file in the csharp\examples\basic\build directory, or click Run in Visual Studio to run the sample directly.

    run csharp build basic samples

The cpp/resources directory contains the model and resource files needed for the sample to run. Ensure that they are in a location that is properly accessible to the program.

Advanced Sample (Multi-Thread Inference)

Compile the Sample

  1. Copy the mmind_dl_sdk_csharp.dll in the csharp/lib folder to the directory where the multi_thread_infer.sln solution file of the multi-thread inference sample is stored (that is, csharp/examples/advanced/multi_thread_infer).

  2. Double-click the multi_thread_infer.sln file to open it in Visual Studio.

  3. On the toolbar of Visual Studio, set Solution Configuration to Release and Solution Platform to x64. In the Visual Studio menu bar, select Build  Build Solution.

  4. After the compilation, you can see the generated executable file in the csharp\examples\advanced\multi_thread_infer\build directory. The example_csharp_multi_thread.exe is used as an example in this section.

Run the Sample

  1. Copy the hasp_rt.exe file and all .dll files in the cpp/bin directory to the directory where the generated example_csharp_multi_thread.exe file is stored.

  2. Copy the resources folder in the cpp folder to the directory where the multi_thread_infer.sln file is stored.

  3. In the csharp\examples\advanced\multi_thread_infer\build directory, double-click the generated example_csharp_multi_thread.exe file or run the sample directly in Visual Studio.

    run csharp multi thread samples

Advanced Sample (OpenCV)

Install OpenCV

Refer to Install OpenCV to complete the OpenCV installation.

Compile the Sample

  1. Copy the mmind_dl_sdk_csharp.dll file in the csharp/lib folder to the directory where the infer_with_opencv.sln solution file of the OpenCV sample is stored (that is, csharp/examples/advanced/infer_with_opencv).

  2. Double-click the infer_with_opencv.sln file to open it in Visual Studio.

  3. On the toolbar of Visual Studio, set Solution Configuration to Release and Solution Platform to x64. In the Visual Studio menu bar, select Build  Build Solution.

  4. After the compilation, you can see the generated executable file in the csharp\examples\advanced\infer_with_opencv\build directory.
    The example_csharp_opencv.exe is used as an example in this section.

Run the Sample

  1. Copy the hasp_rt.exe file and all .dll files in the cpp/bin directory to the directory where the generated example_csharp_opencv.exe file is stored.

  2. Copy the resources folder in the cpp folder to the directory where infer_with_opencv.sln is stored.

  3. In the csharp\examples\advanced\infer_with_opencv\build directory, double-click the generated example_csharp_opencv.exe file or run the sample directly in Visual Studio.

    run example csharp opencv

Advanced Sample (HALCON)

Install HALCON

Refer to Install HALCON to finish HALCON installation.

Compile the Sample

  1. Copy the mmind_dl_sdk_csharp.dll file in the csharp/lib folder to the directory where infer_with_halcon.sln, the solution file of the HALCON sample program, is stored (that is, csharp/examples/advanced/infer_with_halcon).

  2. Double-click the infer_with_halcon.sln file to open it in Visual Studio.

  3. On the toolbar of Visual Studio, set Solution Configuration to Release and Solution Platform to x64. In the Visual Studio menu bar, select Build  Build Solution.

  4. After the compilation, you can see the generated executable file in the csharp\examples\advanced\infer_with_halcon\build directory.
    The example_csharp_halcon.exe is used as an example in this section.

Run the Sample

  1. Copy the hasp_rt.exe file and all .dll files in the cpp/bin directory to the directory where the generated example_csharp_halcon.exe file is stored.

  2. Copy the resources folder in the cpp folder to the directory where infer_with_halcon.sln is stored.

  3. In the csharp\examples\advanced\infer_with_halcon\build directory, double-click the generated example_csharp_halcon.exe file or run the sample directly in Visual Studio.

    run example csharp halcon

FAQs

Compilation Issues

Missing .dll Errors

  • Problem: Compilation failed, indicating missing .dll file.

  • Solution: Confirm that mmind_dl_sdk_csharp.dll in the csharp/lib folder has been copied to the folder where the .sln file is stored.

Could Not Find .NET SDK

  • Problem: The .NET SDK could not be found during compilation.

  • Solution: When installing Visual Studio, ensure that the .NET Desktop Development Workload under the Desktop & Mobile category is selected, and that the .NET SDK component displayed during installation is also selected.

Operation Issues

Missing .dll Errors

  • Problem: The program failed to run, indicating that a .dll file is missing.

  • Solution: Confirm that the hasp_rt.exe file and all .dll files in the cpp/bin directory are copied to the directory where the generated .exe file is stored.

Missing resources or model

  • Problem: A resource or model was missing when the program was running.

  • Solution: Confirm that the cpp/resources folder has been copied to the directory where the .sln file is stored and that this folder contains the model and resource files needed to run the sample.

Is this page helpful?

You can give a feedback in any of the following ways:

We Value Your Privacy

We use cookies to provide you with the best possible experience on our website. By continuing to use the site, you acknowledge that you agree to the use of cookies. If you decline, a single cookie will be used to ensure you're not tracked or remembered when you visit this website.