[英]How can I convert an ushort 16 bit array to an image C#
我有一个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位,有没有办法做到这一点? 谢谢
根据链接的答案改编如何将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位图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.