Haris Iltifat
Haris Iltifat

Reputation: 534

Writing 16-bit image to DICOM file format using GDCM

I have successfully written an 8-bit image as a DICOM file using GDCM library. Now I am trying to write 16-bit image as a DICOM file. Here is my code. Could anyone point out what I am missing or what should be done.

static public void CreateSmallDICOMShort(string fileName, short [] buffer, uint[] dim)
  {
      using (var writer = new gdcm.PixmapWriter())
      {
          gdcm.Pixmap img = writer.GetImage();
          img.SetNumberOfDimensions(3);
          var pixelFormat = new PixelFormat();
          pixelFormat.SetScalarType(PixelFormat.ScalarType.UINT16);
          img.SetDimension(0, dim[0]);
          img.SetDimension(1, dim[1]);
          img.SetDimension(2, 1); // fake a 3d volume
          img.SetPixelFormat(pixelFormat);
          PhotometricInterpretation pi = new PhotometricInterpretation(PhotometricInterpretation.PIType.MONOCHROME1);
          img.SetPhotometricInterpretation(pi);
          gdcm.DataElement pixeldata = new gdcm.DataElement(new gdcm.Tag(0x7fe0, 0x0010));
          //byte[] buffer = new byte[512 * 512 * 2];
          pixeldata.SetArray(buffer,(uint)buffer.Length);
          img.SetDataElement(pixeldata);

          gdcm.File file = writer.GetFile();
          gdcm.DataSet ds = file.GetDataSet();
          gdcm.DataElement ms = new gdcm.DataElement(new gdcm.Tag(0x0008, 0x0016));
          string mediastorage = "1.2.840.10008.5.1.4.1.1.7.2"; // Multi-frame Grayscale Byte Secondary Capture Image Storage
          byte[] val = StrToByteArray(mediastorage);
          ms.SetByteValue(val, new gdcm.VL((uint)val.Length));
          ds.Insert(ms);

          writer.SetFileName(fileName);
          writer.Write();
      }
  }

Upvotes: 3

Views: 1950

Answers (2)

malat
malat

Reputation: 12510

You need to use the other SetArray() function. You are passing an 8-bits data array for the image buffer. Simply pass a 16-bits array. Eg:

unsigned short[] buffer = new unsigned short[512 * 512];
pixeldata.SetArray(buffer,(uint)buffer.Length);

Upvotes: 1

michael pan
michael pan

Reputation: 599

have you tried setting the BitsAllocated, HighBit, and BitsStored tags?

http://www.leadtools.com/sdk/medical/dicom-spec17.htm

Upvotes: 0

Related Questions