rexcardan / Evil-DICOM

A C# DICOM Library
171 stars 98 forks source link

Image problem #47

Closed aaronsimon2 closed 7 years ago

aaronsimon2 commented 7 years ago

I am putting the data from a bitmapped image into my DICOM and it turns out like this. Thanks!

github upload

This is the relevant code.

   Image image = Image.FromFile(phantomFile.Text);             
                image.RotateFlip(RotateFlipType.Rotate180FlipX);

                var elements = new List<IDICOMElement>
                {
                    #region patient
                    new LongString(TagHelper.PATIENT_ID, patientsID.Text),
                    new LongString(TagHelper.TYPE_OF_PATIENT_ID, patientIDType.Text),
                    new PersonName(TagHelper.PATIENT_NAME, patientsName.Text),
                    new AgeString(TagHelper.PATIENT_AGE, patientsAge.Text),
                    new LongString(TagHelper.PATIENT_BIRTH_DATE, patientsDOB.Text ),
                    new CodeString(TagHelper.PATIENT_SEX, patientsSex.Text),
                    #endregion
                    #region study
                    new LongString(TagHelper.STUDY_INSTANCE_UID, studyIdentifier.Text),
                    new LongString(TagHelper.STUDY_ID, studyID.Text),
                    new LongString(TagHelper.STUDY_DATE, studyDate.Text),
                    new LongString(TagHelper.STUDY_TIME, studyTime.Text),
                    new LongString(TagHelper.REFERRING_PHYSICIAN_NAME, referringPhysiciansName.Text),
                    new LongString(TagHelper.ACCESSION_NUMBER, accessionNumber.Text),
                    #endregion
                    #region series
                    new LongString(TagHelper.MODALITY, modality.Text),
                    new LongString(TagHelper.SERIES_INSTANCE_UID, seriesInstanceUID.Text),
                    new LongString(TagHelper.SERIES_NUMBER, seriesNumber.Text),
                    #endregion
                    #region equipment
                    new LongString(TagHelper.CONVERSION_TYPE, conversionType.Text),
                    #endregion
                    #region image
                    new IntegerString(TagHelper.INSTANCE_NUMBER, instanceNumber.Text),
                    new OtherByteString(TagHelper.PIXEL_DATA, imageToByte(image)),
                    new UnsignedShort(TagHelper.ROWS,image.Height),
                    new UnsignedShort(TagHelper.COLUMNS,image.Width),
                    new UnsignedShort(TagHelper.BITS_ALLOCATED, 8),
                    new UnsignedShort(TagHelper.BITS_STORED, 8),
                    new UnsignedShort(TagHelper.HIGH_BIT, 7),
                    new UnsignedShort(TagHelper.PIXEL_REPRESENTATION, 0),
                    new DecimalString(TagHelper.WINDOW_CENTER, 128),
                    new DecimalString(TagHelper.WINDOW_WIDTH, 256),
                    new LongString(TagHelper.PHOTOMETRIC_INTERPRETATION, "MONOCHROME2"),
                    new UnsignedShort(TagHelper.SAMPLES_PER_PIXEL,1),
                   // new DecimalString(TagHelper.PIXEL_SPACING, 0),

                    #endregion
                };

                DICOM obj = new DICOM(elements);

                obj.Write(outputDirectory.Text + "/" + fileName.Text + ".dcm");
   public byte[] imageToByte(Image toConvert)
        {
            MemoryStream stream = new MemoryStream();
            toConvert.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            Byte[] bytes = stream.ToArray();
            return bytes;
        }
rexcardan commented 7 years ago

You mean the problem is the square and the lines overlaid on the image? I am not sure I understand the problem. Your code looks ok except I don't know what phantomFile.Text is. I'm assuming it is a bmp file.

aaronsimon2 commented 7 years ago

The image has been cut and half of it has been put on the other side. When i save the image before i convert it to a byte array it turns out as it should. I can't send a picture of what it should look like because i'm not at work just now but the right hand side of the picture should be on the left.

aaronsimon2 commented 7 years ago

and yes, phantomFile.text is the text inside the image selector box

rexcardan commented 7 years ago

I can't help on this issue without some more info. I need to see what it looks like, and also it would be great if I could get an anonymized file I could debug.

aaronsimon2 commented 7 years ago

Hi Rex. Thanks for the reply. I actually managed to fix this problem. The problem was that the header of the BMP file was being included in the pixel data. I just removed the header from the start and it worked fine!