SimpleITK/GettingStarted/A visual guide to SimpleITK with CSharp: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
No edit summary
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
In this guide we will show how to setup a C# project in Microsoft Visual Studio 2012 which used the available build binaries for SimpleITK. The same steps and options are needed for the other versions of Visual Studio.
{{SimpleITKMigrationHeader}}
 
[[Image: Simpleitk_logo.png‎|center]]
 
In this guide we will show how to setup a C# project in Microsoft Visual Studio 2012 which uses the available built binaries for SimpleITK. The same steps and options are needed for the other versions of Visual Studio.


=Download=
=Download=
[[File:SimpleITK_vs_csharp_visual_fig1.PNG|200px|thumb|Download an unzip the CSharp distribution.]]


{|
|
| [[Image:SimpleITK_vs_csharp_visual_fig1.PNG|left|200px|thumb|Download an unzip the CSharp distribution.]]
Binary downloads are readily available for C# for Microsoft Visual Studio. They are available on [https://sourceforge.net/projects/simpleitk/files/SimpleITK/ SourceForge].
Binary downloads are readily available for C# for Microsoft Visual Studio. They are available on [https://sourceforge.net/projects/simpleitk/files/SimpleITK/ SourceForge].


Select the correct download for you architecture you are going to target.
* Download the correct binary for you architecture you are going to target.


C# for SimpleITK has two components: Native and Managed. The native code contains the SimpleITK C++ library and is compiled for the particular architecture. There is the "win32" for the Intel x86 32-bit architecture, and the "win64" for the Intel x64 architecture. The correct architecture needs to be chosen.
C# for SimpleITK has two components: Native and Managed. The native code contains the SimpleITK C++ library and is compiled for the particular architecture. There is the "win32" for the Intel x86 32-bit architecture, and the "win64" for the Intel x64 architecture. The correct architecture needs to be chosen.


Unzip downloaded zip file into you "Documents" folder. Inside you will find two "dll" files: "SimpleITKCSharpManaged.dll" and "SimpleITKCSharpNative.dll", as well as some documentation files.
* Unzip the downloaded zip file into your "Documents" folder.
 
Inside you will find two "dll" files: "SimpleITKCSharpManaged.dll" and "SimpleITKCSharpNative.dll", as well as some documentation files.
|}


=Adding C# SimpleITK to a Project=
=Adding C# SimpleITK to a Project=
[[File:SimpleITK_vs_csharp_visual_fig2.PNG|200px|thumb|Create a C# Console Application.]]


We will start off with a new C# console solution. This is created by selecting "File->New->Project", then selecting under Templates "Visual C#" and then choosing the "Console Application".
{|
|
|[[Image:SimpleITK_vs_csharp_visual_fig2.PNG|left|200px|thumb|Create a C# Console Application.]]
We will start off with a new C# console solution.  
 
* This is created by selecting "File->New->Project", then selecting under Templates "Visual C#" and then choosing the "Console Application".
 
|}
 


==Select Architecture==
==Select Architecture==


[[File:SimpleITK_vs_csharp_visual_fig3a.PNG|200px|thumb|The default configuration and platform in the toolbar.]]
{|
 
|[[Image:SimpleITK_vs_csharp_visual_fig3a.PNG|left|200px|thumb|The default configuration and platform in the toolbar.]]
[[File:SimpleITK_vs_csharp_visual_fig3b.PNG|200px|thumb|The Configuration Manager.]]
|
The SimpleITK binary only supports a single architecture platform. Your project should be configured to match that same platform.


The SimpleITK binary only support a single platform architecture. You project should be configured to match that platform.
By default, in the Toolbar "Debug" is selected for the Solution Configuration and "Any CPU" is selected for the Solution Platform, this needs to be changed.
|-
|[[Image:SimpleITK_vs_csharp_visual_fig3b.PNG|left|200px|thumb|The Configuration Manager.]]
|


By default, in the Toolbar "Debug" is selected for the Solution Configuration and "Any CPU" is selected for the Solution Platform.
* Bring up the "Configuration Manager" dialog from the menu "BUILD->Configuration Manger...".


Bring up the "Configuration Manager" dialog from the menu "BUILD->Configuration Manger...".
* The architecture of the SimpleITK binary needs to be added, and the "Any CPU" architecture needs to be removed. This needs to be done for both the "Active solution platforms" and the "Platform".


The architecture of the binary needs to be added, and the "Any CPU" architecture needs to be removed. This needs to be done for both the "Active solution platforms" and the "Platform".
|}




==Add Managed Library==
==Add Managed Library==
[[File:SimpleITK_vs_csharp_visual_fig4.PNG|200px|thumb|Adding managed library as reference.]]


From the menu bar select "PROJECT->Add Reference..." to bring up the Reference Manager. Click "Browse..." and navigate the file system to unzip "SimpleITKCSharpManaged.dll" from the binary download, then click OK to add.
{|
|[[File:SimpleITK_vs_csharp_visual_fig4.PNG|left|200px|thumb|Adding managed library as reference.]]
|
* From the menu bar select "PROJECT->Add Reference..." to bring up the Reference Manager. Click "Browse..." and navigate the file system to unzip "SimpleITKCSharpManaged.dll" from the binary download, then click OK to add.
|}


==Add Native Library==
==Add Native Library==
[[File:SimpleITK_vs_csharp_visual_fig5a.PNG|200px|thumb|Adding Native Library]]
[[File:SimpleITK_vs_csharp_visual_fig5b.PNG|200px|thumb|Configuring properties of native library.]]
From the menu bar select "PROJECT->Add Existing Item...".  Select "Executable Files" for the extension type. Then navigate the file system to the unzipped "SimpleITKCSharpNative.dll" file from the binary download. *IMPORTANT* in the "Add" button's pull down menu select "Add As Link".


In the Solution Explorer right click on the "SimpleITKCSharpNative.dll", and select "Properties". Then for "Build Action", choose "Content", and "Copy to OutputDirectory" choose "Copy always".
{|
|[[File:SimpleITK_vs_csharp_visual_fig5a.PNG|left|200px|thumb|Adding Native Library]]
|
* From the menu bar select "PROJECT->Add Existing Item...".  Select "Executable Files" for the extension type. Then navigate the file system to the unzipped "SimpleITKCSharpNative.dll" file from the binary download. *IMPORTANT* in the "Add" button's pull down menu select "Add As Link".
|-
|[[File:SimpleITK_vs_csharp_visual_fig5b.PNG|left|200px|thumb|Configuring properties of native library.]]
|
* In the Solution Explorer right click on the "SimpleITKCSharpNative.dll", and select "Properties".
* Then for "Build Action", choose "Content", and "Copy to OutputDirectory" choose "Copy always".
|}


=Building an Example=
=Building an Example=


Now that we have configured the project, let up copy a basic SimpleITK example to compile and run. The [https://github.com/SimpleITK/SimpleITK/blob/master/Examples/SimpleGaussian.cs SimpleGaussian in C#] is a simple one to test our configuration. This can just be copied an pasted into the code editor.
{|
|
|[[File:SimpleITK_vs_csharp_visual_fig6.PNG|left|200px|thumb|A successful build of the example.]]
* Now that we have configured the project, let up copy a basic SimpleITK example to compile and run. The [https://github.com/SimpleITK/SimpleITK/blob/master/Examples/SimpleGaussian.cs SimpleGaussian in C#] is a simple one to test our configuration. This can just be copied and pasted into the code editor.


Then from the file menu "BUILD->Build Solution" can be selected.
* Then from the file menu "BUILD->Build Solution" can be selected.


If all the steps were followed correctly you should now have an executable which can be run from the command line or from within Visual Studio with the appropriate arguments provided.
If all the steps were followed correctly you should now have an executable which can be run from the command line or from within Visual Studio with the appropriate arguments provided.
|}

Latest revision as of 18:18, 13 December 2019

NOTICE
This page is being migrated to Read The Docs!

For the up to date version please see this page!

Simpleitk logo.png

In this guide we will show how to setup a C# project in Microsoft Visual Studio 2012 which uses the available built binaries for SimpleITK. The same steps and options are needed for the other versions of Visual Studio.

Download

Download an unzip the CSharp distribution.

Binary downloads are readily available for C# for Microsoft Visual Studio. They are available on SourceForge.

  • Download the correct binary for you architecture you are going to target.

C# for SimpleITK has two components: Native and Managed. The native code contains the SimpleITK C++ library and is compiled for the particular architecture. There is the "win32" for the Intel x86 32-bit architecture, and the "win64" for the Intel x64 architecture. The correct architecture needs to be chosen.

  • Unzip the downloaded zip file into your "Documents" folder.

Inside you will find two "dll" files: "SimpleITKCSharpManaged.dll" and "SimpleITKCSharpNative.dll", as well as some documentation files.

Adding C# SimpleITK to a Project

Create a C# Console Application.

We will start off with a new C# console solution.

  • This is created by selecting "File->New->Project", then selecting under Templates "Visual C#" and then choosing the "Console Application".


Select Architecture

The default configuration and platform in the toolbar.

The SimpleITK binary only supports a single architecture platform. Your project should be configured to match that same platform.

By default, in the Toolbar "Debug" is selected for the Solution Configuration and "Any CPU" is selected for the Solution Platform, this needs to be changed.

The Configuration Manager.
  • Bring up the "Configuration Manager" dialog from the menu "BUILD->Configuration Manger...".
  • The architecture of the SimpleITK binary needs to be added, and the "Any CPU" architecture needs to be removed. This needs to be done for both the "Active solution platforms" and the "Platform".


Add Managed Library

Adding managed library as reference.
  • From the menu bar select "PROJECT->Add Reference..." to bring up the Reference Manager. Click "Browse..." and navigate the file system to unzip "SimpleITKCSharpManaged.dll" from the binary download, then click OK to add.

Add Native Library

Adding Native Library
  • From the menu bar select "PROJECT->Add Existing Item...". Select "Executable Files" for the extension type. Then navigate the file system to the unzipped "SimpleITKCSharpNative.dll" file from the binary download. *IMPORTANT* in the "Add" button's pull down menu select "Add As Link".
Configuring properties of native library.
  • In the Solution Explorer right click on the "SimpleITKCSharpNative.dll", and select "Properties".
  • Then for "Build Action", choose "Content", and "Copy to OutputDirectory" choose "Copy always".

Building an Example

A successful build of the example.
  • Now that we have configured the project, let up copy a basic SimpleITK example to compile and run. The SimpleGaussian in C# is a simple one to test our configuration. This can just be copied and pasted into the code editor.
  • Then from the file menu "BUILD->Build Solution" can be selected.

If all the steps were followed correctly you should now have an executable which can be run from the command line or from within Visual Studio with the appropriate arguments provided.