[Insight-users] Simple-ITK installation Tutorial for C#

Dan Mueller dan.muel at gmail.com
Fri Jun 15 22:06:58 EDT 2012


Hi Federico,

The error you are receiving gives the hint:
    "An attempt was made to load a program with an incorrect format."

This error occurs when a .NET program tries to access functions in a
dll which was built using a different architecture; see for example
here:
    http://blogs.msdn.com/b/arvindsh/archive/2009/06/21/tip-of-the-day-an-attempt-was-made-to-load-a-program-with-an-incorrect-format-net-p-invoke-issue.aspx

The easiest way to resolve this is to use the same building settings
as SimpleITK. For example, if you downloaded
"SimpleITK-0.4.0-CSharp-Win64-anycpu.zip" then your calling program
should be built with x64 AnyCPU settings. If you downloaded
"SimpleITK-0.4.0-CSharp-Win32-x86.zip" you should use x86 Win32
settings.

To answer your other question: you only need to add
"SimpleITKCSharpManaged.dll" to your program references, but
"SimpleITKCSharpNative.dll" must be in the probe path (the easiest way
of achieving this is to put it in your binary directory next to your
program executable and the managed dll). This is explained at the
following page (this page is for Linux, but the same approach is
needed for Windows also):
    http://www.itk.org/Wiki/ITK/Release_4/SimpleITK/GettingStarted/Visual_guide_to_building_on_Linux#A_simple_C.23_program

Please let us know how you get on.

HTH

Cheers, Dan

On 15 June 2012 23:33, Federico J. Lopez Bertoni <fjlb at pol.una.py> wrote:
> Hi,
>
> I'm transcribing my post on stackoverflow where I couldn't get any
> help. http://stackoverflow.com/questions/10826487/simple-itk-installation-tutorial-for-c-sharp
>
> I've used ITK with c++ some time ago and I'm trying to use simple itk in c#
> now. I've looked at the MICCAI 2011 tutorial and I'm trying to get started.
> I'm using a Windows 7 64 bit machine and Visual Studio Ultimate 2010.
> According to this wiki I should be able to just unzip and reference the
> .dlls to my project and it should be working. I'm able to add the
> SimpleITKCSharpManaged.dll to my references. However I'm not able to add the
> SimpleITKCSharpNative.dll to my references. It gives me the error: Please
> make sure the file is accesible, and that it is a valid assembly or COM
> component.
>
> I then try to run a simple console app
>
> using System;
>
> using System.Collections.Generic;
>
> using System.IO;
>
> using System.Linq;
>
> using System.Text;
>
> using itk.simple;
>
>
> namespace TestSimpleItk
>
> {
>
>     class Program
>
>     {
>
>         static void Main(string[] args)
>
>         {
>
>             string inputFilename = "001.png";
>
>             if (File.Exists(inputFilename))
>
>             {
>
>                 try
>
>                 {
>
>                     var input = SimpleITK.ReadImage(inputFilename);
>
>                     var binary = SimpleITK.BinaryThreshold(input, 100, 255,
> 255, 0);
>
>                     SimpleITK.WriteImage(binary, "cthead1-binary.png");
>
>                 }
>
>                 catch (Exception ex)
>
>                 {
>
>                     Console.WriteLine("{0}", ex);
>
>                 }
>
>
>             }
>
>             else
>
>             {
>
>                 Console.WriteLine("File '{0}' does not exist",
> inputFilename);
>
>             }
>
>         }
>
>     }
>
> }
>
>
> The exception thrown is: {"The type initializer for
> 'itk.simple.SimpleITKPINVOKE' threw an exception."} and the inner exception
> is: {"An attempt was made to load a program with an incorrect format.
> (Exception from HRESULT: 0x8007000B)"}
>
> I've tried to copy the SimpleITKCSharpNative.dll to my bin/debug directory
> and I'm still getting the same error.
>
> Any ideas on how to solve this, or how to get SimpleItk up and running
> without actually building it locally from the source code using CMAKE?
>
> Thanks
>
>
> Federico


More information about the Insight-users mailing list