Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read TIFF header File c#?

Tags:

c#

header

tiff

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#?

like image 622
Amir Jalali Avatar asked Sep 13 '25 19:09

Amir Jalali


2 Answers

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

        }
    }
}
like image 97
Gustavo F Avatar answered Sep 15 '25 08:09

Gustavo F


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);
like image 41
John Koerner Avatar answered Sep 15 '25 10:09

John Koerner