2013-05-03 145 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上找到。