我正在尝试以下代码来将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问题。这使用了一个不同的库,错误也是不同的。我怀疑这些问题的答案是不同的,因此这些不是重复的问题。此外,另一个问题还没有任何答案。
发布于 2019-10-04 22:21:35
这可能是因为垃圾收集器销毁了图像对象。位图仅包含对图像像素数据的引用,以节省空间。
这在这里已经描述过了: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核心作为启动项目,而引用fo-dicom的库使用.NET标准,那么你必须在程序的开始部分添加以下两项中的一项:
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 serviceshttps://stackoverflow.com/questions/56612349
复制相似问题