简体   繁体   English

如何将Ushort 16位数组转换为图像C#

[英]How can I convert an ushort 16 bit array to an image C#

I have a 16 bit ushort array with values ranging from 0 to 65535 which I want to convert into a grayscale Image to save. 我有一个16位的ushort数组,其值范围从0到65535,我想将其转换为要保存的灰度图像。 What I tried doing was write the values into an image data type and then converting the image to a bitmap, but as soon as I put it into the bitmap data type it gets converted to 8 bit data. 我尝试做的是将值写入图像数据类型,然后将图像转换为位图,但是一旦将其放入位图数据类型,它便立即转换为8位数据。

using (Image<Gray, ushort> DisplayImage2 = new Image<Gray, ushort>(Width, Height))
{
    int Counter = 0;
    for (int i = 0; i < Height; i++)
    {
        for (int j = 0; j < Width; j++)
        {
            DisplayImage.Data[i, j, 0] = ushortArray[Counter];
            Counter++;
        }
    }
    Bitmap bitt = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
    bitt = DisplayImage2.ToBitmap();
    bitt.Save(SaveDirectory, System.Drawing.Imaging.ImageFormat.Tiff);)
}

As soon as the image gets put into the bitmap bitt it gets changed to 8 bit, is there a way of doing this? 一旦将图像放入位图bitt中,就将其更改为8位,有没有办法做到这一点? Thank you 谢谢

Adapting from the linked answer on how to store a Format16bppGrayScale bitmap as TIFF, but without creating the actual bitmap first. 根据链接的答案改编如何将Format16bppGrayScale位图存储为TIFF,但无需先创建实际位图。 This requires some .NET dlls that you may not usually add as references, namely PresentationCore and WindowsBase. 这需要一些您通常不会添加为引用的.NET dll,即PresentationCore和WindowsBase。

The BitmapSource necessary to construct the BitmapFrame that the TIFF encoder can encode can be created straight from an array, so: 可以直接从数组创建构造TIFF编码器可以编码的BitmapFrame所需的BitmapSource ,因此:

var bitmapSrc = BitmapSource.Create(Width, Height, 96, 96,
                                    PixelFormats.Gray16, null, rawData, Width * 2);
TiffBitmapEncoder encoder = new TiffBitmapEncoder();
encoder.Compression = TiffCompressOption.Zip;
encoder.Frames.Add(BitmapFrame.Create(bitmapSrc));
encoder.Save(outputStream);

When I tried this, the file seemed to be a real 16bit image. 当我尝试此操作时,该文件似乎是真实的16位图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM