繁体   English   中英

Fo-dicom 代码编译但不执行

[英]Fo-dicom code compiling but not executing

我正在尝试以下代码将 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 

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

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

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

这之前已经在这里描述过: C# System.Drawing.Image.get_Width() throws exception on WinForms form is maximized

要解决此问题,请将其用作:

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

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

但是,有时甚至会更棘手:例如,如果您使用 .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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM