# CSharp Integration¶

## Code¶

using System;
using System.Runtime.InteropServices;

using itk.simple;
using PixelId = itk.simple.PixelIDValueEnum;

namespace itk.simple.examples {
public class Program {
static void Main(string[] args) {

if (args.Length < 1) {
Console.WriteLine("Usage: SimpleGaussian <input>");
return;
}

// Cast to we know the the pixel type
input = SimpleITK.Cast(input, PixelId.sitkFloat32);

// calculate the number of pixels
VectorUInt32 size = input.GetSize();
int len = 1;
for (int dim = 0; dim < input.GetDimension(); dim++) {
len *= (int)size[dim];
}
IntPtr buffer = input.GetBufferAsFloat();
// Note: C# also has a GetConstBufferAs... methods which do not
// implicitly call MakeUnique.

// There are two ways to access the buffer:

// (1) Access the underlying buffer as a pointer in an "unsafe" block
// (note that in C# "unsafe" simply means that the compiler can not
// perform full type checking), and requires the -unsafe compiler flag
// unsafe {
//   float* bufferPtr = (float*)buffer.ToPointer();

//   // Now the byte pointer can be accessed as per Brad's email
//   // (of course this example is only a 2d single channel image):
//   // This is a 1-D array but can be access as a 3-D. Given an
//   // image of size [xS,yS,zS], you can access the image at
//   // index [x,y,z] as you wish by image[x+y*xS+z*xS*yS],
//   // so x is the fastest axis and z is the slowest.
//   for (int j = 0; j < size; j++) {
//     for (int i = 0; i < size; i++) {
//       float pixel = bufferPtr[i + j*size];
//       // Do something with pixel here
//     }
//   }
// }

// (2) Copy the buffer to a "safe" array (i.e. a fully typed array)
// (note that this means memory is duplicated)
float[] bufferAsArray = new float[len]; // Allocates new memory the size of input
Marshal.Copy(buffer, bufferAsArray, 0, len);
double total = 0.0;
for (int j = 0; j < size; j++) {
for (int i = 0; i < size; i++) {
float pixel = bufferAsArray[i + j*size];
total += pixel;
}
}
Console.WriteLine("Pixel value total: {0}", total);

}
}
}
{"ft_lang_label":"__label__en","ft_lang_prob":0.511014,"math_prob":0.9784549,"size":2059,"snap":"2020-45-2020-50","text_gpt3_token_len":580,"char_repetition_ratio":0.11532847,"word_repetition_ratio":0.074285716,"special_character_ratio":0.3355998,"punctuation_ratio":0.18765432,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99647003,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-24T18:14:45Z\",\"WARC-Record-ID\":\"<urn:uuid:ce1bae03-b620-4b1e-8860-a7ef7df25285>\",\"Content-Length\":\"24166\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6b6d0b0b-d471-45bd-8217-dc41313ea698>\",\"WARC-Concurrent-To\":\"<urn:uuid:b92110b8-1e05-4f2e-be41-1a0626a9fb7a>\",\"WARC-IP-Address\":\"104.17.33.82\",\"WARC-Target-URI\":\"https://simpleitk.readthedocs.io/en/master/link_CSharp_docs.html\",\"WARC-Payload-Digest\":\"sha1:U3YGWIYDFLNIP5OGKVZCE76X44KUZFHX\",\"WARC-Block-Digest\":\"sha1:WMES4RBRZBJML2PQZKBW7T4F7G66R5L4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107884322.44_warc_CC-MAIN-20201024164841-20201024194841-00435.warc.gz\"}"}