我有一个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位,有没有办法做到这一点?谢谢
答案 0 :(得分:0)
从linked answer开始适应如何将Format16bppGrayScale
位图存储为TIFF,但没有先创建实际位图。这需要一些您通常不会添加为引用的.NET dll,即PresentationCore和WindowsBase。
构造TIFF编码器可以编码的BitmapSource
所必需的BitmapFrame
可以直接从数组中创建,因此:
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位图像。