Use Intel RealSense camera in .NET 5 project
Tags: dotnet, realsense, csharp, mixedreality
Published: 2021-08-18 20:32:30

**Prerequisite software tool**

1. [Visual Studio 2019](https://visualstudio.microsoft.com/vs) with **.NET desktop development** & **.NET cross-platform development** & **Desktop development with C++** workload:  
![visual studio 2019 install workload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmfvueu5b6rdpkzktinf.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/km7dynledo9wyl51n2g0.png)
or Use [downloaded SDK installer](https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk).
3. [CMake](https://cmake.org) 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](https://github.com/IntelRealSense/librealsense).
2. Generate Visual Studio solution files in git repository top level directory via [Developer Command Prompt/PowerShell for VS 2019](https://docs.microsoft.com/en-us/visualstudio/ide/reference/command-prompt-powershell?view=vs-2019):  
    ```powershell
    mkdir build
    cd build
    cmake .. -DBUILD_CSHARP_BINDINGS=ON -DBUILD_SHARED_LIBS=ON
    ```
    Generated files are inside the `build` directory:
    ![CMake file generated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpviyo17jl31vdrnd81z.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hs5c6o32vw79p0nmmd7h.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ykzzdovd9unc9enmaewo.png)
5. Right click and select build to start building native RealSense C++ library dll for Windows:
![Build C++ native lib](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sg3qounyo631e997bhm7.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2cicrzg79h0j4iqd5xa.png)
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:  
    ```XML
    <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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cxlvt6jposqjjutq8t2.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t6ofsoavwvfufatlk7b2.png)

---

**.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](https://docs.microsoft.com/en-us/dotnet/standard/frameworks#net-5-os-specific-tfms) to `net5.0-windows`.
2. Add [RuntimeIdentifier](https://docs.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxz250747kjoekx7sx9d.png)