CameraSDK : C# (Windows)
We provide .NET language bindings based on .NET Standard 2.0. Even though .NET is cross platform, our .NET assembly is not. Since it depends directly on unmanaged code compiled for Windows.
Prerequisites
Windows 10 (x64)
.NET Standard 2.0 compatible .NET SDK
MSVC Redistributable 2015-2022
The C# libraries are provided as part of our standard Windows library ZIP packages. You can download the package, containing both C# and C++ libraries at the Download page.
Tip
If you do not have the Microsoft Visual C++ toolchain installed, you will need to download and install the redistributable runtime.
Download the newest runtime from Microsoft here: Latest Visual C++ Redistributable downloads.
Setup Your Project
To account for those who do not use Visual Studio, we use the command line .NET Core utility dotnet
in this tutorial.
Open up PowerShell or command prompt, and navigate to the location where you want to put our sample project. Initialize a new .NET Core console based application:
PS> dotnet new console -n helloworld
Here we called our sample project helloworld
.
Now, cd
into the helloworld
directory. Copy your downloaded CameraSDK zip file into the project folder and extract the archive. If you use PowerShell you can:
PS> Expand-Archive .\CameraSdk-Win_Release_2.X.X.zip -DestinationPath .
You will end up with libraries for both C++ and C# inside a folder named: dest
. The resulting directory layout should be:
dest
|-- Cs
|-- include
`-- lib
We do not need neither the include
nor the lib
subdirectories, you can delete those if you want to.
Project File
The dotnet
util created a project file for us: helloworld.csproj
. We need to add the CameraSDK assembly to this, and also add a post-build copy step. This should copy the file CameraSdkCBindingsForCs.dll
to the build output directory. This file is needed by the CameraSDK library assembly.
Add the CameraSDK C# library assembly and dependency to helloworld.csproj
. The xml snippet below should be appended inside the existing Project
tag:
<ItemGroup>
<!-- Include CameraSDK C# API -->
<Reference Include="CameraSdkCs.dll">
<HintPath>dest/Cs/CameraSdkCs.dll</HintPath>
</Reference>
<!-- Dependency of CameraSDK C# API -->
<None Include="dest/Cs/CameraSdkCBindingsForCs.dll" Link="CameraSdkCBindingsForCs.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
We added both the assembly (CameraSdkCs.dll
) for the C# API and its dependency, a DLL file compiled by the Visual C++ compiler (CameraSdkCBindingsForCs.dll
).
Note
Despite of the both files having the .dll
extension, they are not the same format. One is a .NET assembly file, and the other is a standard dynamic library file with plain C functions inside.
Source Code
The console application template include a sample source file: Program.cs
. Replace its content with this:
using System;
using System.Collections.Generic;
using System.Linq;
using P1.CameraSdk;
namespace helloworld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Probing for available cameras...");
IEnumerable<CameraDescriptor> cameras = Camera.GetAvailableCameras();
foreach(var camera in cameras)
{
Console.WriteLine("* {0}", camera);
}
if (cameras.Any() == false)
{
Console.WriteLine("Could not find a camera!");
}
}
}
}
The source code will query for any USB connected Phase One cameras. The connected cameras are listed in the console, before the program exits.
Build & Run
To compile the project, simply run the build
command:
PS> dotnet build
This will build and copy the needed (assembly’s and DLL’s) to the build output directory. For .NET 5 this is bin/Debug/net5
.
To run the application, first cd
into the build directory and run the executable:
PS> cd bin/Debug/net5
PS> helloworld.exe
Probing for available cameras...
* CameraDesc: name=IQ4 150MP type=USB serial=JD012345 is-open=no
PS>
Warning
We deliberately changed the working directory to the location of the executable. This is because the .NET runtime expects to find the CameraSdkCBindingsForCs.dll
file in the current working directory.
- If we attempted to run the application from the
helloworld
project folder, we would get an exception:: System.DllNotFoundException: C Bindings DLL (CameraSdkCBindingsForCs.dll) not found at: .helloworld