/// <summary> /// returns dicomFile in the content type given by finalContentType in a HttpResponseMessage. /// If content type is dicom, transfer syntax must be set to the given transferSyntax parameter. /// </summary> /// <param name="dicomFile"></param> /// <param name="finalContentType"></param> /// <param name="transferSyntax"></param> /// <returns></returns> private HttpResponseMessage ReturnImageAsHttpResponse(DicomFile dicomFile, string finalContentType, string transferSyntax) { MediaTypeHeaderValue header = null; Stream streamContent = null; if (finalContentType == JpegImageContentType) { DicomImage image = new DicomImage(dicomFile.Dataset); Bitmap bmp = image.RenderImage(0).As<Bitmap>(); //When an image/jpeg MIME type is returned, the image shall be encoded using the JPEG baseline lossy 8 //bit Huffman encoded non-hierarchical non-sequential process ISO/IEC 10918. //TODO Is it the case with default Jpeg format from Bitmap? header = new MediaTypeHeaderValue(JpegImageContentType); streamContent = new MemoryStream(); bmp.Save(streamContent, ImageFormat.Jpeg); streamContent.Seek(0, SeekOrigin.Begin); } else if (finalContentType == AppDicomContentType) { //By default, the transfer syntax shall be //"Explicit VR Little Endian". //Note: This implies that retrieved images are sent un-compressed by default. DicomTransferSyntax requestedTransferSyntax = DicomTransferSyntax.ExplicitVRLittleEndian; if (transferSyntax != null) requestedTransferSyntax = GetTransferSyntaxFromString(transferSyntax); bool transferSyntaxIsTheSameAsSourceFile = dicomFile.FileMetaInfo.TransferSyntax == requestedTransferSyntax; //we only change transfer syntax if we need to DicomFile dicomFileToStream; if (!transferSyntaxIsTheSameAsSourceFile) { dicomFileToStream = dicomFile.ChangeTransferSyntax(requestedTransferSyntax); } else { dicomFileToStream = dicomFile; } header = new MediaTypeHeaderValue(AppDicomContentType); streamContent = new MemoryStream(); dicomFileToStream.Save(streamContent); streamContent.Seek(0, SeekOrigin.Begin); } HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(streamContent); result.Content.Headers.ContentType = header; return result; }
/// <summary> /// Convert the byte data retrieved from DCM file to Image /// </summary> /// <param name="imagedata"></param> /// <returns></returns> private Bitmap ConvertDCMData2BitImage(DicomFile dcm) { DicomFile newDcmFile = null; if (dcm.FileMetaInfo.TransferSyntax.IsEncapsulated)//if the data is compressed { // System.Reflection.Assembly.LoadFrom(Path.Combine(Application.StartupPath,"Dicom.Native64.dll")); DicomTranscoder.LoadCodecs(Application.StartupPath, "Dicom.Native*.dll"); newDcmFile=dcm.ChangeTransferSyntax(DicomTransferSyntax.ExplicitVRLittleEndian,new DicomJpegLsParams()); } DicomImage imageDcm=null; if (newDcmFile != null) imageDcm = new DicomImage(newDcmFile.Dataset); else imageDcm = new DicomImage(dcm.Dataset); DicomDataset dataset = dcm.Dataset; byte[] fs = imageDcm.PixelData.NumberOfFrames < 2 ? imageDcm.PixelData.GetFrame(0).Data : imageDcm.PixelData.GetFrame(1).Data; uint size = (uint)Marshal.SizeOf(typeof(short)); uint padding = (uint)fs.Length % size; uint count = (uint)fs.Length / size; short[] values = new short[count]; System.Buffer.BlockCopy(fs, 0, values, 0, (int)(fs.Length - padding)); int height = dataset.Get<int>(DicomTag.Rows); int width = dataset.Get<int>(DicomTag.Columns); int windowsWidth = (int)dataset.Get<double>(DicomTag.WindowWidth); int windowsCenter = (int)dataset.Get<double>(DicomTag.WindowCenter); //if the windowsWidth = 0, the DCM file is not standard type. if (windowsWidth == 0 || windowsCenter==0) { windowsWidth = values.Max<short>()-values.Min<short>(); windowsCenter = windowsWidth / 2; } int low = windowsCenter - windowsWidth / 2; int high = windowsCenter + windowsWidth / 2; Bitmap bitmap = new Bitmap(width, height); for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { int r, g, b; int temp = (int)values[(width - j - 1) * height + i]; int val = temp > high ? 255 : (temp < low ? 0 : ((temp - low) * 255 / windowsWidth)); r = g = b = val; bitmap.SetPixel(i, width-j-1, Color.FromArgb(r, g, b)); } } return bitmap; }