一、读取16位TIFF图片的灰度值
1.直接从文件读取成数组
public static ushort[] Get16TiffBmpPixels(string tiffpath)
{
if (!File.Exists(tiffpath)) return null;
try
{
Stream imageStreamSource = new FileStream(tiffpath, FileMode.Open, FileAccess.Read, FileShare.Read);
var decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bmpsrc = decoder.Frames[0];
int stride=bmpsrc.PixelWidth*bmpsrc.Format.BitsPerPixel/8;
ushort[] pixels = new ushort[bmpsrc.PixelWidth * bmpsrc.PixelHeight];
bmpsrc.CopyPixels(pixels, stride, 0);
return pixels;
}
catch
{
throw;
}
}
/// <summary>
/// 获取图片所有像素
/// </summary>
/// <param name="wb"></param>
/// <returns></returns>
public static ushort[] GetWriteableBitmapAllPixels(WriteableBitmap wb)
{
try
{
int stride = wb.PixelWidth * wb.Format.BitsPerPixel / 8;
ushort[] tmppixels = new ushort[stride * wb.PixelHeight];
wb.CopyPixels(tmppixels, stride, 0);
return tmppixels;
}
catch
{
throw;
}
}
/// <summary>
/// 将ushort数组转成byte数组
/// </summary>
/// <param name="uarr"></param>
/// <returns></returns>
public static byte[] ConvertushortArrayTobyteArray(ushort[] uarr)
{
byte[] byteArray = new byte[uarr.Length*2];
IntPtr intptr = Marshal.UnsafeAddrOfPinnedArrayElement(uarr, 0);
Marshal.Copy(intptr, byteArray, 0, byteArray.Length);
return byteArray;
}
二、根据灰度数组创建16位TIFF图
比如从相机拿到的16位灰度值数组,或者从图像中拿到的数组,需要根据这些数据创建图像。
//byte数组
public static WriteableBitmap Create16BitGrayBitmap(byte[] rawValues,int width,int height)
{
WriteableBitmap wb = new WriteableBitmap(width, height, 96, 96, System.Windows.Media.PixelFormats.Gray16, BitmapPalettes.Gray16);
System.Windows.Int32Rect rect=new System.Windows.Int32Rect(0,0,width,height);
int stride = width * wb.Format.BitsPerPixel / 8;
wb.WritePixels(rect, rawValues, stride, 0);
return wb;
}
//ushort数组
public static WriteableBitmap Create16BitGrayBitmap(ushort[] rawValues,int width,int height)
{
WriteableBitmap wb = new WriteableBitmap(width, height, 96, 96, System.Windows.Media.PixelFormats.Gray16, BitmapPalettes.Gray16);
System.Windows.Int32Rect rect=new System.Windows.Int32Rect(0,0,width,height);
int stride = width * wb.Format.BitsPerPixel / 8;
wb.WritePixels(rect, rawValues, stride, 0);
return wb;
}
public static BitmapSource Create16BitGrayBitmapSource(byte[] rawValues, int width, int height)
{
int stride = width *16 / 8;
BitmapSource source = BitmapSource.Create(width, height, 96, 96, System.Windows.Media.PixelFormats.Gray16, BitmapPalettes.Gray16, rawValues, stride);
return source;
}
三、将图片保存成TIFF文件
/// <summary>
/// 将BitmapSource保存到本地Tiff文件
/// </summary>
/// <param name="source"></param>
/// <param name="tiffpath"></param>
/// <returns></returns>
public static bool SaveBitmapToTiffFile(BitmapSource source,string tiffpath)
{
try
{
var stream = new FileStream(tiffpath, FileMode.Create);
var encoder = new TiffBitmapEncoder();
encoder.Compression = TiffCompressOption.Zip;
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(stream);
stream.Close();
stream.Dispose();
return true;
}
catch
{
throw ;
}
}
public static bool SaveBitmapToTiffFile(WriteableBitmap source, string tiffpath)
{
try
{
var stream = new FileStream(tiffpath, FileMode.Create);
var encoder = new TiffBitmapEncoder();
encoder.Compression = TiffCompressOption.Zip;
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(stream);
stream.Close();
stream.Dispose();
return true;
}
catch
{
throw ;
}
}