Search code examples
c#asp.netdicomfo-dicom

Fellow Oak DICOM - changing image window level


I am not an experienced programmer, just need to add a DICOM viewer to my VS2010 project. I can display the image in Windows Forms, however can't figure out how to change the window center and width. Here is my script:

DicomImage image = new DicomImage(_filename);
            int maxV = image.NumberOfFrames;
            sbSlice.Maximum = maxV - 1;
            image.WindowCenter = 7.0;
            double wc = image.WindowCenter;
            double ww = image.WindowWidth;

            Image result = image.RenderImage(0);
            DisplayImage(result);

It did not work. I don't know if this is the right approach.


Solution

  • I looked at the code and it looked extremely buggy. https://github.com/rcd/fo-dicom/blob/master/DICOM/Imaging/DicomImage.cs

    In the current buggy implementation setting the WindowCenter or WindowWidth properties has no effect unless Dataset.Get(DicomTag.PhotometricInterpretation) is either Monochrome1 or Monochrome2 during Load(). This is already ridiculous, but it still cannot be used because the _renderOptions variable is only set in a single place and is immediately used for the _pipeline creation (not giving you chance to change it using the WindowCenter property). Your only chance is the grayscale _renderOptions initialization: _renderOptions = GrayscaleRenderOptions.FromDataset(Dataset);.

    The current solution: Your dataset should have

    • DicomTag.WindowCenter set appropriately
    • DicomTag.WindowWidth != 0.0
    • DicomTag.PhotometricInterpretation == Monochrome1 or Monochrome2

    The following code accomplishes that:

    DicomDataset dataset = DicomFile.Open(fileName).Dataset;
    //dataset.Set(DicomTag.WindowWidth, 200.0); //the WindowWidth must be non-zero
    dataset.Add(DicomTag.WindowCenter, "100.0");
    //dataset.Add(DicomTag.PhotometricInterpretation, "MONOCHROME1"); //ValueRepresentations tag is broken
    dataset.Add(new DicomCodeString(DicomTag.PhotometricInterpretation, "MONOCHROME1"));
    DicomImage image = new DicomImage(dataset);
    image.RenderImage();
    

    The best solution: Wait while this buggy library is fixed.