convert SimpleITK image to activiz vtkImageData in CSharp

Hello, everyone.
I’m trying to convert SimpleITK image to activiz vtkImageData in CSharp using vtkImageImport.
I compared eyery pixel value after convert itk.simple.image.It seems ok but when I want to use the new vtkImageData, exception thrown:“ System.AccessViolationException ”(located at Kitware.VTK.dll )

Here is my code.

ImageSeriesReader dicomReader = new ImageSeriesReader();
VectorString filenames = ImageSeriesReader.GetGDCMSeriesFileNames(“F:/tmp/l”);
dicomReader.SetFileNames(filenames);
itk.simple.Image image = dicomReader.Execute();

VectorDouble direction = image.GetDirection();
VectorDouble origin = image.GetOrigin();
VectorDouble spacing = image.GetSpacing();
VectorUInt32 size = image.GetSize();

ImageFileWriter writer1 = new ImageFileWriter();
writer1.SetFileName(“F:/tmp/image1.mhd”);
writer1.Execute(image);

string typeString = image.GetPixelIDTypeAsString();//“16-bit unsigned integer”

int len = 1;
for (int dim = 0; dim < image.GetDimension(); dim++)
{
len *= (int)size[dim];
}

IntPtr imageptr = image.GetBufferAsUInt16();
vtkImageImport imageImport = new vtkImageImport();
//imageImport.SetImportVoidPointer(imageptr);
imageImport.CopyImportVoidPointer(imageptr, len * 2);//2bytes
imageImport.SetDataScalarTypeToUnsignedShort();
imageImport.SetWholeExtent(0, (int)size[0] - 1, 0, (int)size[1] - 1, 0, (int)size[2] - 1);
imageImport.SetDataExtentToWholeExtent();
imageImport.SetDataSpacing(spacing[0], spacing[1], spacing[2]);
imageImport.Update();

imageData = imageImport.GetOutput();
imageData.SetOrigin(origin[0], origin[1], origin[2]);
imageData.SetSpacing(spacing[0], spacing[1], spacing[2]);
imageData.SetExtent(0, (int)size[0] - 1, 0, (int)size[1] - 1, 0, (int)size[2] - 1);
imageData.SetNumberOfScalarComponents(1);

for (uint i = 0; i < size[0]; i++)
{
for (uint j = 0; j < size[1]; j++)
{
for (uint k = 0; k < size[0]; k++)
{
VectorUInt32 indexVector = new VectorUInt32 { i,j,k};
UInt16 pixelValue = image.GetPixelAsUInt16(indexVector);
if(pixelValue!= imageData.GetScalarComponentAsDouble((int)i, (int)j, (int)k, 0))
{
MessageBox.Show(“not equal”);
return;
}
}
}
}

vtkMetaImageWriter writer = new vtkMetaImageWriter();
writer.SetInput(imageData);
writer.SetFileName(@“F:/tmp/image2.mhd”);
writer.Update();//System.AccessViolationException

It is difficult to see some people use activiz for development. There is too little information in this regard. It is too exciting to see someone asking questions. I am not alone. I use the following code for itk to vtk images. Conversion, it works very well.You may need to pay attention to the problem of image flipping. Hope it helps you.

public vtkImageData Excute(sitk.Image itkImage)
{
var width = (int)itkImage.GetWidth();
var height = (int)itkImage.GetHeight();
var depth = (int)itkImage.GetDepth();
var numberOfPoints = itkImage.GetNumberOfPixels();
var origin = itkImage.GetOrigin();
var spacing = itkImage.GetSpacing();
vtkImage.SetOrigin(origin[0], origin[1], origin[2]);
vtkImage.SetSpacing(spacing[0], spacing[1], spacing[2]);
vtkImage.SetExtent(0, width - 1, 0, height - 1, 0, depth - 1);
vtkImage.AllocateScalars();
var vp = vtkImage.GetScalarPointer();
var ip = itkImage.GetBufferAsInt16();
var length = (uint)(numberOfPoints * 2);
ip.CopyTo(vp, length);
return vtkImage;
}

    [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
    public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

    public static void CopyTo(this IntPtr src, IntPtr dest, uint len)
    {
        CopyMemory(dest, src, len);
    }

Thanks for your reply.
Glad to meet you!

I tested your code, but got the same result as before.
Maybe there is a problem with my visual studio project settings.Or I don’t understand the vtkImageData correctly.

May I ask how you handle the “vtkImage” in your code?
I just initialized it like “new vtkImageData” and did some properties modify.

        vtkImage = vtkImageData.New();
        vtkImage.SetScalarTypeToShort();
        vtkImage.SetNumberOfScalarComponents(1);

Thanks.
It looks same.I will search the cause in other respects.