简体   繁体   English

Fo-dicom 代码编译但不执行

[英]Fo-dicom code compiling but not executing

I am trying following code to convert a dicom file to jpeg:我正在尝试以下代码将 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");
        }}

I am compiling it with following command:我正在使用以下命令编译它:

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

The code compiles without any error but on running the exe file, it gives following error:代码编译没有任何错误,但在运行 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 

Where is the problem and how can it be solved?问题出在哪里,如何解决? Thanks for your help.谢谢你的帮助。

Edit: I am having similar problem with another library for which I have posted another question.编辑:我在另一个图书馆遇到了类似的问题,我已经发布了另一个问题。 This is using a different library and the error is also different.这是使用不同的库,错误也不同。 I suspect answers to these questions will be different hence these are not duplicate question.我怀疑这些问题的答案会有所不同,因此这些不是重复的问题。 Moreover, the other question does not have any answer yet.此外,另一个问题还没有任何答案。

This is probably due to the garbage collector destroying the image object.这可能是由于垃圾收集器破坏了图像对象。 The Bitmap only contains a reference to the pixel data of the image to save space.位图仅包含对图像像素数据的引用以节省空间。

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

To solve this, use it as this:要解决此问题,请将其用作:

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

By cloning, a real copy of the pixel data is performed.通过克隆,执行像素数据的真实副本。

However, it could be sometimes even trickier: If you eg use .NET Core as a starting project and the library, which references fo-dicom uses .NET Standard, you have to add either of these to the start of your program:但是,有时甚至会更棘手:例如,如果您使用 .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