在C#中解析TIFF文件

所有,

我有一个标准的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]); } } } 

您可以将文件作为位图加载,然后在位图或LockBits函数上使用GetPixel。 GetPixel很简单但很慢。 LockBits有点复杂但速度超快。

代码就像是

var bitmap = new Bitmap(fileName)bitmap.GetPixel(0,0)

对于锁定位,只需进行网络搜索就有很多文章。

Bob Powell有很多关于此的信息:

还有TIFF库可以帮助:

另外看一下MSDN指南: