How to read TIFF header File c#?

天大地大妈咪最大 提交于 2019-12-07 16:28:19

问题


I wanna know how it is possible to read a file in binary format.

for example a tiff image file may have the following binary format in hex 0000 4949 002A 0000. how can i get these values in c#?


回答1:


Here is how I usually read files in hexadecimal format, changed for the header, as you need:

using System;
using System.Linq;
using System.IO;

namespace FileToHex
{
    class Program
    {
        static void Main(string[] args)
        {
            //read only 4 bytes from the file

            const int HEADER_SIZE = 4;

            byte[] bytesFile = new byte[HEADER_SIZE];

            using (FileStream fs = File.OpenRead(@"C:\temp\FileToHex\ex.tiff"))
            {
                fs.Read(bytesFile, 0, HEADER_SIZE);
                fs.Close();
            }

            string hex = BitConverter.ToString(bytesFile);

            string[] header = hex.Split(new Char[] { '-' }).ToArray();

            Console.WriteLine(System.String.Join("", header));

            Console.ReadLine();

        }
    }
}



回答2:


You can use the ReadAllBytes method of the System.IO.File class to read the bytes into an array:

System.IO.FileStream fs = new System.IO.FileStream(@"C:\Temp\sample.pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read);
int size = 1024;
byte[] b = new byte[size];
fs.Read(b, 0, size);



回答3:


I have not used LibTIFF.Net, http://bitmiracle.com/libtiff but it seems to be fairly complete.

Using it, instead of reading the file as bytes and then decoding the header(s) may be a lot easier for you.



来源:https://stackoverflow.com/questions/9071581/how-to-read-tiff-header-file-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!