ImageSDK : 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 ImageSDK zip file into the project folder and extract the archive. If you use PowerShell you can:

PS> Expand-Archive .\ImageSdk-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 ImageSDK assembly to this, and also add a post-build copy step. This should copy the file ImageSdkCBindingsForCs.dll to the build output directory. This file is needed by the ImageSDK library assembly.

Add the ImageSDK C# library assembly and dependency to helloworld.csproj. The xml snippet below should be appended inside the existing Project tag:

<ItemGroup>

      <!-- Include ImageSDK C# API -->
      <Reference Include="ImageSdkCs.dll">
          <HintPath>dest/Cs/ImageSdkCs.dll</HintPath>
      </Reference>

      <!-- Dependency of ImageSDK C# API -->
      <None Include="dest/Cs/ImageSdkCBindingsForCs.dll" Link="ImageSdkCBindingsForCs.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>

      <!-- Include stitching and stacking DLLs, since these are hard deps -->
      <Content Include="dest/Cs/libifcoremd.dll" Link="libifcoremd.dll" Visible="false">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
      <Content Include="dest/Cs/libifportmd.dll" Link="libifportmd.dll" Visible="false">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
      <Content Include="dest/Cs/libmmd.dll" Link="libmmd.dll" Visible="false">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
      <Content Include="dest/Cs/P1CIRSTACK.dll" Link="P1CIRSTACK.dll" Visible="false">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
      <Content Include="dest/Cs/Qt5Core.dll" Link="Qt5Core.dll" Visible="false">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>

  </ItemGroup>

We added both the assembly (ImageSdkCs.dll) for the C# API and its dependency, a DLL file compiled by the Visual C++ compiler (ImageSdkCBindingsForCs.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 others are 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 P1.ImageSdk;

namespace helloworld
{
    class Program
    {
       static void Main(string[] args)    
        {
            // Setup the path to the SensorProfiles or copy the directory where the sensor profiles are located to where you program is executed
            Sdk.SetSensorProfilesLocation("./SensorProfiles");

            Console.WriteLine("Open IIQ file");

            // Open an IIQ-file (edit the name and path to an IIQ-file)
            RawImage iiq = new RawImage("input-file.iiq");

            // Setup a convert config with the description about how to convert image into Rgb domain                
            ConvertConfig config = new ConvertConfig();
            config.SetOutputScale(.25);

            Console.WriteLine("Do the conversion...");

            P1.ImageSdk.BitmapImage bitmap = iiq.Convert(config);

            Console.WriteLine("Write image to tiff file...");

            // The resulting bitmap can now be stored to a tiff file.            
            TiffConfig tiffConfig = new TiffConfig();
            _ = iiq.WriteAsTiff("output-file.tiff", bitmap, tiffConfig);

            Console.WriteLine("Done!");
        }
    }
}

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
Open IIQ file
Do the conversion...
Write image to tiff file...
Done!
PS>

Warning

We deliberately changed the working directory to the location of the executable. This is because the .NET runtime expects to find the ImageSdkCBindingsForCs.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 (ImageSdkCBindingsForCs.dll) not found at: .\helloworld