C# wpf 读写16位tif图灰度数据

这篇博客介绍了如何处理16位TIFF图像,包括从文件读取灰度值数组,创建16位灰度图像,以及将图像保存为TIFF文件的方法。提供了使用WriteableBitmap和BitmapSource的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、读取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 ;
            }
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值