# C# Equivalent of GetArray(ViewFrom)Image

**URL:** <https://discourse.itk.org/t/c-equivalent-of-getarray-viewfrom-image/7313>\
**Category:** Beginner Questions\
**Tags:** csharp\
**Created:** [November 14, 2024, 12:40pm UTC](https://discourse.itk.org/t/c-equivalent-of-getarray-viewfrom-image/7313 "2024-11-14T12:40:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![general\_rishkin](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/a9a28c/32.png) [@general\_rishkin](https://discourse.itk.org/u/general_rishkin)\
**Post date:** [November 14, 2024, 12:40pm UTC](https://discourse.itk.org/t/c-equivalent-of-getarray-viewfrom-image/7313/1 "2024-11-14T12:40:31Z")

</div>

I am attempting to follow the [SimpleITK2020 (Python) tutorial](https://github.com/SimpleITK/TUTORIAL/blob/07f19971b5f7f7be72efce6ecb0a09015be253d4/02_images_and_resampling.ipynb) but with F#.

Part of the code in the “Accessing Pixels and Slicing” section is the following:

```auto
logo_copy[115:190, 0:height] = logo_copy[190:115:-1, 0:height]
plt.imshow(sitk.GetArrayViewFromImage(logo_copy))

```

What is the equivalent of:

- `GetArrayViewFromImage` and `GetArrayFromImage` in C# and
- the array slicing `logo_copy[115:190, 0:height] = logo_copy[190:115:-1, 0:height]` in C#?

---

<div class="post-metadata">

**Author:** ![dchen](https://discourse.itk.org/user_avatar/discourse.itk.org/dchen/32/34_2.png) [@dchen](https://discourse.itk.org/u/dchen)\
**Post date:** [November 14, 2024, 1:08pm UTC](https://discourse.itk.org/t/c-equivalent-of-getarray-viewfrom-image/7313/2 "2024-11-14T13:08:45Z")

</div>

For C# the following example shows how you can get a pointer to the image buffer and copy out the pixels:

[https://simpleitk.readthedocs.io/en/master/link\_CSharp\_docs.html](https://simpleitk.readthedocs.io/en/master/link_CSharp_docs.html)

I don’t think there’s any way of do the type of image slicing in C#.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 14, 2024, 2:18pm UTC](https://discourse.itk.org/t/c-equivalent-of-getarray-viewfrom-image/7313/3 "2024-11-14T14:18:00Z")

</div>

> [@dchen](#):
>
> I don’t think there’s any way of do the type of image slicing in C#.

There is no fancy syntax implemented for image slicing in C# but the filters which implement the operations are available:  
[https://simpleitk.org/doxygen/v2\_0/html/classitk\_1\_1simple\_1\_1SliceImageFilter.html](https://simpleitk.org/doxygen/v2_0/html/classitk_1_1simple_1_1SliceImageFilter.html)  
[https://simpleitk.org/doxygen/v2\_3/html/classitk\_1\_1simple\_1\_1PasteImageFilter.html](https://simpleitk.org/doxygen/v2_3/html/classitk_1_1simple_1_1PasteImageFilter.html)

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [November 14, 2024, 2:18pm UTC](https://discourse.itk.org/t/c-equivalent-of-getarray-viewfrom-image/7313/4 "2024-11-14T14:18:16Z")

</div>

Hello @general_rishkin,

With respect to the slicing flipping and pasting which is shown in that example, you will need to use the filters directly [ExtractImageFilter](https://simpleitk.org/doxygen/v2_4/html/classitk_1_1simple_1_1ExtractImageFilter.html), [FlipImageFilter](https://simpleitk.org/doxygen/v2_4/html/classitk_1_1simple_1_1FlipImageFilter.html), and [PasteImageFilter](https://simpleitk.org/doxygen/v2_4/html/classitk_1_1simple_1_1PasteImageFilter.html). Something along the lines of the following Python code:

```auto
extracted_sub_image = sitk.Extract(logo_copy, size=[75, height], index=[115, 0])
extracted_sub_image_flipped = sitk.Flip(extracted_sub_image, flipAxes=[True, False])
res = sitk.Paste(
    destinationImage=logo_copy,
    sourceImage=extracted_sub_image_flipped,
    sourceSize=extracted_sub_image_flipped.GetSize(),
    sourceIndex=[0] * extracted_sub_image_flipped.GetDimension(),
    destinationIndex=[115, 0],
)

```

---

<div class="post-metadata">

**Author:** ![general\_rishkin](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/a9a28c/32.png) [@general\_rishkin](https://discourse.itk.org/u/general_rishkin)\
**Post date:** [November 15, 2024, 2:40pm UTC](https://discourse.itk.org/t/c-equivalent-of-getarray-viewfrom-image/7313/5 "2024-11-15T14:40:49Z")

</div>

Many thanks all. I was able to use the ExtractImageFilter, the FlipFilter and the PasteImageFilter from F# to reproduce the example particularly following @zivy 's code.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 15, 2024, 2:46pm UTC](https://discourse.itk.org/t/c-equivalent-of-getarray-viewfrom-image/7313/6 "2024-11-15T14:46:56Z")

</div>

Glad to hear you got it working. Just wanted to note that the SliceImageFilter can replace the two ExtractImageFilter and FlipImageFilter, if you want to optimize 🙂
