【问题标题】:How can I convert an ushort 16 bit array to an image C#如何将 ushort 16 位数组转换为图像 C#
【发布时间】:2019-03-23 09:36:07
【问题描述】:

我有一个 16 位 ushort 数组,其值范围从 0 到 65535,我想将其转换为灰度图像以保存。我尝试将值写入图像数据类型,然后将图像转换为位图,但一旦我将其放入位图数据类型,它就会转换为 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);)
}

一旦图像被放入位图bitt,它就会变成8位,有没有办法做到这一点?谢谢

【问题讨论】:

  • 我知道,这就是为什么我也尝试将其保存为 24 位,但最后也保存为 8 位。
  • 显示你从哪里得到这个数组,如果它有任何标题,任何特定格式。
  • 该数组来自相机帧抓取,它只是 0 到 65535 之间的值而已。
  • 不一定是 tiff,只要是支持 16 位图像的任何文件格式即可。
  • 相机图像可能是原始格式,与16位位图格式无关。 16 位位图格式过时,会丢失颜色信息。

标签: c# image bitmap pixelformat ushort


【解决方案1】:

改编自linked answer,了解如何将Format16bppGrayScale 位图存储为 TIFF,但无需先创建实际位图。这需要一些您通常不会添加为引用的 .NET dll,即 PresentationCore 和 WindowsBase。

构造 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);

当我尝试这个时,该文件似乎是一个真正的 16 位图像。

【讨论】:

    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 2015-05-02
    • 2016-04-30
    • 2014-08-18
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多