Question

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

Was it helpful?

Solution

Dan Mueller from ITK answered my question. Here's what he had to say:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top