# SimpleITK write Image to Dicom

**URL:** https://discourse.itk.org/t/simpleitk-write-image-to-dicom/5594
**Category:** Beginner Questions
**Tags:** csharp, dicom, simpleitk
**Created:** [December 26, 2022, 1:48am UTC](https://discourse.itk.org/t/simpleitk-write-image-to-dicom/5594 "2022-12-26T01:48:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jeffppp](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jeffppp](https://discourse.itk.org/u/jeffppp)
#### Post date: [December 26, 2022, 1:48am UTC](https://discourse.itk.org/t/simpleitk-write-image-to-dicom/5594/1 "2022-12-26T01:48:10Z")

</div>

I had read a series Dicom in Image class and resample the pixel to 1x1x1. I want to write the resampled Image to Dicom. But I only found the example in python. In C#, how should I split the 3D image to 2D image like image[:,:,i] in python? Or how can I write the Dicom in C# slice by slice?

thanks.

---

<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: [December 26, 2022, 3:04pm UTC](https://discourse.itk.org/t/simpleitk-write-image-to-dicom/5594/2 "2022-12-26T15:04:54Z")

</div>

Hello @jeffppp,

You are on the right path. Follow the [Python and R example](https://simpleitk.readthedocs.io/en/master/link_DicomSeriesReadModifyWrite_docs.html), just replace the bracket operator with the use of the [ExtractImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ExtractImageFilter.html).

---

<div class="post-metadata">

### Author: ![jeffppp](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jeffppp](https://discourse.itk.org/u/jeffppp)
#### Post date: [December 29, 2022, 1:47am UTC](https://discourse.itk.org/t/simpleitk-write-image-to-dicom/5594/3 "2022-12-29T01:47:02Z")

</div>

Thanks for your help.I can write Image to dicom, but I have another problem.  
I’m not sure why I get the strange image position. I want to write [0,0,1], [0,0,2]…  
But I get the the same value [0;2.5464e-313;4.3501e-312],[0;2.5464e-313;4.3501e-312] …

How should I change my code?

> var idx = optimg.TransformIndexToPhysicalPoint(new VectorInt64() { 0, 0, i }).ToArray(); nImage.SetMetaData(“0020|0032”, string.Format(“{0},{1},{2}”,idx[0], idx[1], idx[2]));

I had tried this

> nImage.SetMetaData(“0020|0032”, “0,0,”,i.ToString());

But the results are the same. QQ

---

<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: [December 29, 2022, 2:08pm UTC](https://discourse.itk.org/t/simpleitk-write-image-to-dicom/5594/4 "2022-12-29T14:08:22Z")

</div>

Hello @jeffppp,

Setting the Image Position Patient attribute,0020|0032, will not have an effect as it is obtained directly from the image, its origin, when written to file. If you resampled the original volume correctly, then when you use the `ExtractImageFilter` the slice origin is valid and when you write it to file the Image Position Patient attribute will have the correct value.

Please confirm that the resampled volume has the expected origin, spacing and direction cosine matrix (`GetOrigin()`, `GetSpacing`, `GetDirection`). Then check the origin of the image obtained from the `ExtractImageFilter`, it should match what you expect without you needing to set anything explicitly.

---

<div class="post-metadata">

### Author: ![jeffppp](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@jeffppp](https://discourse.itk.org/u/jeffppp)
#### Post date: [January 9, 2023, 2:18am UTC](https://discourse.itk.org/t/simpleitk-write-image-to-dicom/5594/5 "2023-01-09T02:18:04Z")

</div>

Hello @zivy

I used another method to test my code.  
I read the dicom to the Image. And I didn’t do anything.  
Just write the Image to dicom directly.  
I had checked the extracted Images, the parameters (`GetOrigin()` , `GetSpacing` , `GetDirection` ). seems normal.  
But I check the dicom metadata in matlab, the ImageOrientationPatient is [1;0;0;0;1;0], and the ImagePositionPatient is [-118.1749;2.2493e-312;9.5065e-312] \<== just the first number is normal.

How can I fix this problem?

thanks.

```auto
ExtractImageFilter extract = new ExtractImageFilter();
            Image newimg = new Image(image.GetSize()[0], image.GetSize()[1], PixelIDValueEnum.sitkFloat32);
            for (int i=0;i<176;i++)
            {

                extract.SetSize(new VectorUInt32() { image.GetSize()[0], image.GetSize()[1] , 0});
                extract.SetIndex(new VectorInt32() { 0, 0, i });
                extract.SetDirectionCollapseToStrategy(ExtractImageFilter.DirectionCollapseToStrategyType.DIRECTIONCOLLAPSETOSUBMATRIX);
                newimg = extract.Execute(image);
                var origin1 = newimg.GetOrigin();
                var direction1 = newimg.GetDirection();
                var pixelspace = newimg.GetSpacing();
                CastImageFilter castImageFilter = new CastImageFilter();
                castImageFilter.SetOutputPixelType(PixelIDValueEnum.sitkInt16);
                Image optimg = castImageFilter.Execute(newimg);
                optimg.SetMetaData("0010|0010","HYMA");
                optimg.SetMetaData("0010|0020", "T123");
                optimg.SetMetaData("0010|0030", "19870602");
                optimg.SetMetaData("0020|000D", "1.2.840.113619.2.182.10124123185175.1646284464.128677");
                optimg.SetMetaData("0020|0010", "223331417X01");
                optimg.SetMetaData("0008|0020", "20230107");
                optimg.SetMetaData("0008|0030", "135745.312000");
                optimg.SetMetaData("0008|0050", "223331417X01");
                optimg.SetMetaData("0008|0060", "MR");
                optimg.SetMetaData("0008|0031", "145118.859000");
                optimg.SetMetaData("0008|0021", "20220303");
                optimg.SetMetaData("0008|0008", "DERIVED\\SECONDARY");
                optimg.SetMetaData("0008|0012", "145118.859000");
                optimg.SetMetaData("0008|0013", "20220303");
                optimg.SetMetaData("0020|000e", "1.3.12.2.1107.5.2.38.51042.202203031451095637489111.0.0.0");
                optimg.SetMetaData("0020|0032", (image.TransformContinuousIndexToPhysicalPoint(new VectorDouble() {0,0,i })[0]).ToString()+";"+ (image.TransformContinuousIndexToPhysicalPoint(new VectorDouble() { 0, 0, i })[1]).ToString()+";"+ (image.TransformContinuousIndexToPhysicalPoint(new VectorDouble() { 0, 0, i })[2]).ToString());
                optimg.SetMetaData("0020|0013", (i+1).ToString());
                //optimg.SetMetaData("0020|0037", image.GetDirection()[0].ToString()+","+ image.GetDirection()[3].ToString()+","+ image.GetDirection()[6].ToString()+"," + image.GetDirection()[1].ToString() + "," + image.GetDirection()[4].ToString() + "," + image.GetDirection()[7].ToString());
                optimg.SetMetaData("0020|0037", "0.993076233113407,0.0244321788116105,-0.1149028453251,-0.0242702610276,0.999701489765079,0.00280816508323");

                ImageFileWriter writer = new ImageFileWriter();
                writer.KeepOriginalImageUIDOn();
                writer.SetFileName(@"E:\CT_Dicom\Re\image_" + i.ToString() + ".dcm");
                writer.Execute(optimg);
                
            }

```
