我如何保存/加载.NET的x64 16位图像?
-
21-09-2019 - |
题
在,当我使用Win32,我用的FreeImage 以便加载和保存位的位图深度大于8位。这是与每一个形象我的工作,因为我在做医疗影像和任何人说任何事情之前,是的,我和我的客户已经花了很多钱,高亮度,高对比度的显示器用动态范围的11或12位。事实上,如果你很好奇,由ACR要求运行乳腺X线摄影 incldue一监视与动态范围至少10位。
我刚切换到64位的内存开销,并得到所有我发展到一个平台和编译模式。我宁愿不回Win32和我的客户都在那里与我(和真正迫使变化)。 FreeImage的不会编译在64位的Windows;它在代码编译器就不能处理_asm指令。
我想我会尝试在微软的类原生.NET支持。长话短说:他们没有工作,和失败,非常有限的错误消息。我怀疑这是因为微软仍然不支持Format16bppGrayScale类。
也许有在我的代码有问题。这里是我的代码写:
Bitmap theBitmap = new Bitmap(inImage.XSize, inImage.YSize, PixelFormat.Format16bppGrayScale);
//have to go with lockbits
Rectangle rect = new Rectangle(0, 0, theBitmap.Width, theBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData =
theBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
PixelFormat.Format16bppGrayScale);
IntPtr ptr = bmpData.Scan0;
int theByteSize = theBitmap.Width * theBitmap.Height *2;
byte[] theByteBuffer = new byte[theByteSize];
System.Buffer.BlockCopy(inImage.Data, 0, theByteBuffer, 0, theByteSize);
System.Runtime.InteropServices.Marshal.Copy(theByteBuffer, 0, ptr, theByteSize);
theBitmap.UnlockBits(bmpData);
theBitmap.Save(inDirectory + "\\" + inName);
theBitmap.Dispose();
此代码崩溃程序与
An unhandled exception of type
'System.Runtime.InteropServices.ExternalException' occurred in
System.Drawing.dll
Additional information: A generic error occurred in GDI+.
有趣的,尤其是因为我从来没有想这个图像绘制到这样的画面(虽然这将是很好!),只是想用SAVE / LOAD功能。图像不被写入磁盘(即使该程序崩溃),和下面的读代码也崩溃该程序:
Bitmap theBitmap = new Bitmap(theCompleteName, false);
ushort[] theData = new ushort[theBitmap.Width * theBitmap.Height];
int x, y;
switch (theBitmap.PixelFormat)
{
case PixelFormat.Format16bppGrayScale:
//have to go with lockbits
{
Rectangle rect = new Rectangle(0, 0, theBitmap.Width, theBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData =
theBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
PixelFormat.Format16bppGrayScale);
IntPtr ptr = bmpData.Scan0;//scanline approach left over from FreeImage
for (y = 0; y < theBitmap.Height; ++y){
byte[] scanline = new byte[theBitmap.Width*2];
System.Runtime.InteropServices.Marshal.Copy(ptr, scanline, y * theBitmap.Width * 2, theBitmap.Width * 2);
System.Buffer.BlockCopy(scanline, 0, theData, y * theBitmap.Width * 2, theBitmap.Width * 2);
}
theBitmap.UnlockBits(bmpData);
}
break;
//for colors, just take the red and call it a day
case PixelFormat.Format24bppRgb:
case PixelFormat.Format32bppArgb://really stupid reading code, always works
for (y = 0; y < theBitmap.Height; ++y) {
for (x = 0; x < theBitmap.Width; ++x) {
theData[y * theBitmap.Width + x] = (byte)(theBitmap.GetPixel(x, y).R);
}
}
break;
}
theNewImage = new ImageContainer(theData, theBitmap.Width, theBitmap.Height, inName, inAssessmentID);
theBitmap.Dispose();//not needed, anymore
此代码崩溃的程序,发生错误:
An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid.
这些结果告诉我,微软仍然没有固定的PixelFormat枚举的Format16bppGrayScale部分。这是一个耻辱。
所以,我有什么可以用它来加载和保存在x64 16个灰度图像与.NET?
(编辑:我要补充的是,虽然我可以节省出DICOM图像,我需要运行在非患者数据实验,以验证该算法是合理的,等等DICOM需要一组的UID和其他必需的字段这是矫枉过正我需要什么。我只需要图像,而不是患者数据,此刻)
解决方案
的FreeImage 可以被编译到x64。继说明这里你可以绕过_asm
指令。还有就是在页面底部的编译64位的DLL。
在最新版本(3.15.1)已包含此修复程序。我拿起它的源代码尝试(我好奇在我自己的项目中使用的FreeImage)和x64平台编译罚款的时候了。
其他提示
我真的不回答你的问题,但以前使用 Neodnyamic 出售的ImageDraw组件和肯定建议(和使用)再次它。这不是免费的,但非常值得投资小。