2013-05-03 339 views
0
using (Bitmap bmp = (Bitmap)Bitmap.FromFile(C:\Users\112\AppData\Local\Temp\113837.dcm)) 
{ 
    // obtain the XResolution and YResolution TIFFTAG values 
    PropertyItem piXRes = bmp.GetPropertyItem(282); 
    PropertyItem piYRes = bmp.GetPropertyItem(283); 

    // values are stored as a rational number - numerator/denominator pair 
    numerator = BitConverter.ToInt32(piXRes.Value, 0); 
    denominator = BitConverter.ToInt32(piXRes.Value, 4); 
    float xRes = numerator/denominator; 

    numerator = BitConverter.ToInt32(piYRes.Value, 0); 
    denominator = BitConverter.ToInt32(piYRes.Value, 4); 
    float yRes = numerator/denominator; 

    // now set the values 
    byte[] numeratorBytes = new byte[4]; 
    byte[] denominatorBytes = new byte[4]; 

    numeratorBytes = BitConverter.GetBytes(600); // specify resolution in numerator 
    denominatorBytes = BitConverter.GetBytes(1); 

    Array.Copy(numeratorBytes, 0, piXRes.Value, 0, 4); // set the XResolution value 
    Array.Copy(denominatorBytes, 0, piXRes.Value, 4, 4); 

    Array.Copy(numeratorBytes, 0, piYRes.Value, 0, 4); // set the YResolution value 
    Array.Copy(denominatorBytes, 0, piYRes.Value, 4, 4); 

    bmp.SetPropertyItem(piXRes); // finally set the image property resolution 
    bmp.SetPropertyItem(piYRes); 

    bmp.SetResolution(600, 600); // now set the bitmap resolution 

    bmp.Save(@"C:\output.tif"); // save the image 
} 

我在行using (Bitmap bmp = ...上收到“内存不足”错误。我该如何解决这个问题?将Dicom图像导出为tif格式

+0

我认为第一个也是最令人难以置信的明显问题是“Temp \ 113837.dcm'有多大? – 2013-05-03 05:40:02

+0

它可以是任何大小,7 kb到100 MB – 2013-05-03 05:48:42

回答

2

“内存不足”是误导。这实际上意味着图像格式不能由.Net确定。

对不起,但.Net不支持DICOM图像。有关支持的图像格式的信息,请参见http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx

+0

啊,没错。我总是会忘记这个奇怪的errno翻译问题。接得好。 – 2013-05-03 06:28:49

+0

太真了。在我的日常工作中,我一直处理DICOM图像,并知道.Net不支持它们。 – 2013-05-03 06:51:52

2

这条线......

(Bitmap)Bitmap.FromFile(C:\Users\112\AppData\Local\Temp\113837.dcm) 

...你正在阅读包含在DICOM文件全部原始数据。这包括Dicom数据元素(包含信息的字段)。

提取图像数据是much more complicated比这个。你应该开始寻找关于the Dicom format的一些信息。

其他良好的信息来源可以在DabsoftMedical Connections上找到,当然也可以在David Clunie的website上找到。