Fo-dicom代码正在编译但未执行

时间:2019-06-15 16:50:23

标签: c# dicom fo-dicom

我正在尝试下面的代码将dicom文件转换为jpeg:

using System;
using System.IO;
using System.Drawing; 
using Dicom.Imaging; 

class RnReadDicom{
    public static void Main(string[] args){
        string fileName = "33141578.dcm"; 
        var image = new DicomImage(fileName);
        image.RenderImage().AsSharedBitmap().Save(@"test.jpg");
        }}

我正在使用以下命令对其进行编译:

$ mcs a.cs -r:Dicom.Core.dll -r:Dicom.Native.dll -r:System.Drawing 

代码编译没有任何错误,但是在运行exe文件时,出现以下错误:

$ ./a.exe 

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'Dicom.DicomEncoding' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object
  at Dicom.IO.IOManager.get_BaseEncoding () [0x00000] in <4b7c269b3e704f3f83dd85bb2721c76a>:0 
  at Dicom.DicomEncoding..cctor () [0x00000] in <4b7c269b3e704f3f83dd85bb2721c76a>:0 
   --- End of inner exception stack trace ---
  at Dicom.Imaging.DicomImage..ctor (System.String fileName, System.Int32 frame) [0x00000] in <4b7c269b3e704f3f83dd85bb2721c76a>:0 
  at RnReadDicom.Main (System.String[] args) [0x00006] in <5c119b113a6e4d4b8058662dd31bab14>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'Dicom.DicomEncoding' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object
  at Dicom.IO.IOManager.get_BaseEncoding () [0x00000] in <4b7c269b3e704f3f83dd85bb2721c76a>:0 
  at Dicom.DicomEncoding..cctor () [0x00000] in <4b7c269b3e704f3f83dd85bb2721c76a>:0 
   --- End of inner exception stack trace ---
  at Dicom.Imaging.DicomImage..ctor (System.String fileName, System.Int32 frame) [0x00000] in <4b7c269b3e704f3f83dd85bb2721c76a>:0 
  at RnReadDicom.Main (System.String[] args) [0x00006] in <5c119b113a6e4d4b8058662dd31bab14>:0 

问题出在哪里,如何解决?感谢您的帮助。

编辑:我对另一个发布了another问题的图书馆也遇到了类似的问题。这使用的是其他库,并且错误也不同。我怀疑这些问题的答案会有所不同,因此这些不是重复的问题。而且,另一个问题还没有任何答案。

1 个答案:

答案 0 :(得分:0)

这可能是由于垃圾收集器破坏了图像对象。位图仅包含对图像像素数据的引用,以节省空间。

之前已经对此进行了描述:C# System.Drawing.Image.get_Width() throws exception on WinForms form is maximized

要解决此问题,请按以下方式使用它:

var image = new DicomImage(fileName);
image.RenderImage().AsClonedBitmap().Save(@"test.jpg");

通过克隆,将执行像素数据的真实副本。

但是,有时可能会更加棘手: G。使用.NET Core作为开始项目以及引用fo-dicom使用.NET Standard的库,您必须将其中两个添加到程序的开头:

    TranscoderManager.SetImplementation(new MonoTranscoderManager()); // limited codecs, but full .NET Core, runs on Linux. Or:
    TranscoderManager.SetImplementation(new DesktopTranscoderManager()); // loads also native dlls, runs on Windows

    ImageManager.SetImplementation(new RawImageManager()); // if you need .NET Core, you only have byte arrays
    ImageManager.SetImplementation(new WinFormsImageManager()); //if you run Windows and have GDI

    NetworkManager.SetImplementation(new DesktopNetworkManager()); // if you want to run dicom services