簡體   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