我如何保存/载入16位图像.NET 64位? [英] How can I save/load a 16 bit image in .net x64?

查看:355
本文介绍了我如何保存/载入16位图像.NET 64位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在,当我用win32,我用 FreeImage的以加载和保存位深度的位图超过800位以上。这是每一个和我一起工作,因为我在做医疗影像图像,任何人说任何事情之前,是的,我和我的客户都花在了高亮度,高对比度的显示器了大量的资金与11或12位的动态范围。事实上,如果你很好奇,要求由ACR运行乳腺X线摄影 incldue显示器具有至少10位的动态范围。

Before, when I was using win32, I used FreeImage in order to load and save bitmaps of bit depth greater than 8 bits. That's every image I work with, since I'm doing medical imaging, and before anyone says anything, yes, me and my customers have spent a lot of money on high-brightness, high-contrast monitors with 11 or 12 bits of dynamic range. In fact, if you're curious, requirements by the ACR for running mammography incldue a monitor with at least 10 bits of dynamic range.

我刚换到x64的内存开销,并让我所有的发展到一个平台和编译模式。我宁愿不回Win32和我的客户在那里与我(和真正强迫的变化)。 FreeImage的没有就64位Windows;它在code编译器就不能处理_asm指令。

I just switched to x64 for the memory overheads and to get all of my development onto one platform and compiling mode. I'd rather not to back to win32, and my customers are right there with me (and really forcing the change). FreeImage does not compile on 64 bit windows; it has a _asm directive in the code that the compiler just can't handle.

我想我会尝试在微软类的本地.NET支持。长话短说:他们没有工作,不能以非常有限的错误消息。我怀疑这是因为微软仍然不支持Format16bppGrayScale类。

I thought I'd try the native .NET support in the Microsoft classes. Long story short: They don't work, and fail with very limited error messages. I suspect it's because Microsoft still doesn't support the Format16bppGrayScale class.

也许有我的code的一个问题。这是我的$ C $下写:

Maybe there's a problem in my code. Here's my code for writing:

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();

这code导致程序崩溃与

This code crashes the program with

An unhandled exception of type 
 'System.Runtime.InteropServices.ExternalException' occurred in 
 System.Drawing.dll

Additional information: A generic error occurred in GDI+.

有趣的,尤其是因为我从来没有想得出这样的形象是这样的画面(虽然这将是很好!),只是想使用保存/载入功能。图像不会被写入磁盘(即使程序崩溃),和下面的阅读code也导致程序崩溃:

Interesting, especially since I never want to draw this image to the screen like this (although it would be nice!), but just want to use the save/load functionality. The image does get written to disk (even though the program crashes), and the following reading code also crashes the program:

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

这code导致程序崩溃的错误:

This code crashes the program with the error:

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

Additional information: Parameter is not valid.

这些结果告诉我,微软仍然没有固定的PixelFormat枚举的Format16bppGrayScale部分。这是一个耻辱。

These results tell me that Microsoft still hasn't fixed the Format16bppGrayScale portion of the PixelFormat enumeration. That's a shame.

所以,我有什么可以用它来加载和保存在x64 16位灰度图像与.NET?

So what can I use to load and save 16 bit grayscale images on x64 with .NET?

(编辑:我要补充一点,虽然我可以节省出DICOM图像,我需要运行在非患者数据的实验来验证算法的声音,等等DICOM需要一组UID和其他所需领域这是矫枉过正我需要什么,我只需要图像,而不是患者数据,目前)

( I should add that while I can save out DICOM images, I need to run experiments on non-patient data to verify that the algorithms are sound, and so forth. DICOM requires a set of UIDs and other required fields that are overkill for what I need; I just need images, and not patient data, at the moment).

推荐答案

FreeImage的的可以的被编译到x64。按照指示这里您可以围绕 _asm 指令得到。此外,还有是编译的64位的dll在页面的底部。

FreeImage can be compiled to x64. Following the instructions here you can get around the _asm directive . There also is a compiled 64-bit dll at the bottom of the page.

最新版本(3.15.1)已经包含此修复程序。我拿起它的源代码,以尝试(我好奇使用FreeImage的在我自己的项目)和x64平台编译罚款的时候了。

The latest version (3.15.1) already contains this fix. I've picked up the source distribution to try (I'm being curious to use FreeImage in my own project) and the x64 platform compiles fine right away.

这篇关于我如何保存/载入16位图像.NET 64位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆