在C#中的TIFF文件解析 [英] TIFF file parsing in C#

查看:2896
本文介绍了在C#中的TIFF文件解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,



我有一个标准的TIFF文件。我需要编写C#代码来读取TIFF文件的每个像素的数据。例如,我不知道数据在这个文件中的开始等等。有人可能指向我一些链接,我可以在C#中获取示例代码。



欣赏

解决方案

我建议使用 TiffBitmapDecoder 类。一旦创建了此类的实例,就可以访问Frames集合。此集合中的每个帧表示TIFF文件中单个图层的位图数据。您可以对各个框架使用 CopyPixels 方法来创建字节数组,



<$ p>

命名空间TiffExample
{
using System;
using System.IO;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;

public static class Program
{
private const int bytesPerPixel = 4; //此常数必须与转换的位图的像素格式相对应。

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();
}

私有静态颜色GetPixel(byte [] bgraBytes,int x,int y,int width)
{
var index =(y * bytesPerPixel)+(x * bytesPerPixel));

return Color.FromArgb(
bgraBytes [index + 3],
bgraBytes [index + 2],
bgraBytes [index + 1],
bgraBytes [index]);
}
}
}


All,

I have a standard TIFF file with me. I need to write C# code to read data for each pixel of the TIFF file. For example, I don't know where the data starts in this file etc.. Can somebody points me to some links where I can get sample code in C#.

Appreciate your help!

解决方案

I would suggest using the TiffBitmapDecoder class. Once you've created an instance of this class, you can access the Frames collection. Each frame in this collection represents the bitmap data for an individual layer in the TIFF file. You can use the CopyPixels method on the individual frames to create byte arrays that represent the pixel data of the image.

Update:

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]);
        }
    }
}

这篇关于在C#中的TIFF文件解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆