在C#中解析TIFF文件
发布时间:2020-12-15 17:28:01 所属栏目:百科 来源:网络整理
导读:所有, 我有一个标准的TIFF文件.我需要编写C#代码来读取TIFF文件的每个像素的数据.例如,我不知道数据在这个文件中的起始位置等等.有人可以指向一些我可以在C#中获取示例代码的链接. 感谢您的帮助! 解决方法 我建议使用 TiffBitmapDecoder课程.一旦创建了此类
所有,
我有一个标准的TIFF文件.我需要编写C#代码来读取TIFF文件的每个像素的数据.例如,我不知道数据在这个文件中的起始位置等等.有人可以指向一些我可以在C#中获取示例代码的链接. 感谢您的帮助! 解决方法
我建议使用
TiffBitmapDecoder课程.一旦创建了此类的实例,就可以访问Frames集合.此集合中的每个帧表示TIFF文件中单个图层的位图数据.您可以在各个帧上使用
CopyPixels方法来创建表示图像像素数据的字节数组.
更新: namespace TiffExample { using System; using System.IO; using System.Windows.Media; using System.Windows.Media.Imaging; public static class Program { private const int bytesPerPixel = 4; // This constant must correspond with the pixel format of the converted bitmap. private static void Main() { var stream = File.Open("example.tif",FileMode.Open); var tiffDecoder = new TiffBitmapDecoder( stream,BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreImageCache,BitmapCacheOption.None); stream.Dispose(); var firstFrame = tiffDecoder.Frames[0]; var convertedBitmap = new FormatConvertedBitmap(firstFrame,PixelFormats.Bgra32,null,0); var width = convertedBitmap.PixelWidth; var height = convertedBitmap.PixelHeight; var bytes = new byte[width * height * bytesPerPixel]; convertedBitmap.CopyPixels(bytes,width * bytesPerPixel,0); Console.WriteLine(GetPixel(bytes,548,314,width)); Console.ReadKey(); } private static Color GetPixel(byte[] bgraBytes,int x,int y,int width) { var index = (y * (width * bytesPerPixel) + (x * bytesPerPixel)); return Color.FromArgb( bgraBytes[index + 3],bgraBytes[index + 2],bgraBytes[index + 1],bgraBytes[index]); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |