DEV Community

Chen Yu Pao
Chen Yu Pao

Posted on • Updated on

Use Intel RealSense camera in .NET 5 project

Prerequisite software tool

  1. Visual Studio 2019 with .NET desktop development & .NET cross-platform development & Desktop development with C++ workload: visual studio 2019 install workload
  2. Windows 10 SDK version 10.0.10586 or later: Can be installed via Individual Components section of Visual Studio 2019 Installer: Install with VS2019 or Use downloaded SDK installer.
  3. CMake and add its executable location to PATH environment variable.

Build native RealSense library & C# wraaper

Build Windows version native RealSense library

  1. On Windows 10 machine that had above Prerequisite software tools installed, Clone the source from Intel RealSense SDK git repository.
  2. Generate Visual Studio solution files in git repository top level directory via Developer Command Prompt/PowerShell for VS 2019:

    mkdir build
    cd build
    cmake .. -DBUILD_CSHARP_BINDINGS=ON -DBUILD_SHARED_LIBS=ON
    

    Generated files are inside the build directory:
    CMake file generated

  3. Open generated build\librealsense2.sln solution file in Visual Studio 2019, if prompt the following dialog, select first option to change target to .NET framework 4.6.1:
    Change target framework of Intel.RealSense project

  4. Select the C++ project in Library/realsense2, then make sure the configuration is set to Release, and x64:
    Set realsense2 visual studio build configuration

  5. Right click and select build to start building native RealSense C++ library dll for Windows:
    Build C++ native lib

  6. It will take quite a long time to build, after successfully build, there should be a realsense2.dll file inside the build\Release folder:
    Native dll release build files location
    This is the native RealSense library for Windows, copy it for later usage.

Build .NET 5 C# wrapper of RealSense SDK

  1. Create a text file called Intel.RealSense.csproj inside the wrappers\csharp\Intel.RealSense folder with following content:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net5.0-windows</TargetFramework>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
      </PropertyGroup>
    </Project>
    
  2. Run dotnet build -c Release in wrappers\csharp\Intel.RealSense directory:
    Build release version of C# wrapper
    And you will find two files Intel.RealSense.dll & Intel.RealSense.pdb inside the wrappers\csharp\Intel.RealSense\bin\Release\net5.0-windows directory, these two are C# wrapper library files, copy them for later usage:
    C# wrapper library files


.NET 5 project usage demo

Demo project is on GitHub: https://github.com/windperson/Net5RealSenseDemo

It is just a .NET 5 console project, but you have to manually modify the .csproj file content:

  1. Set .NET 5+ OS-specific TFMs to net5.0-windows.
  2. Add RuntimeIdentifier to win10-x64
  3. Native RealSense library files need be copied into .NET executable file's folder by MSBuild: Copy native RealSense library to execution folder

Discussion (0)