// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; namespace E { public class TiffDim { public struct HWDim { public int H; public int W; } private static short ReadWord(Stream stm, int ix) { stm.Seek(ix, SeekOrigin.Begin); byte[] word = new byte[2]; stm.Read(word, 0, 2); return BitConverter.ToInt16(word, 0); } private static int ReadDWord(Stream stm, int ix) { stm.Seek(ix, SeekOrigin.Begin); byte[] dword = new byte[4]; stm.Read(dword, 0, 4); return BitConverter.ToInt32(dword, 0); } public static HWDim Find(string fnm) { HWDim res = new HWDim(); using(Stream stm = new FileStream(fnm, FileMode.Open, FileAccess.Read)) { int ix = ReadWord(stm, 4); for(;;) { int ntag = ReadWord(stm, ix); ix += 2; for(int i = 0; i < ntag; i++) { int tagid = ReadWord(stm, ix); if(tagid == 257) { res.H = ReadDWord(stm, ix + 8); } else if(tagid == 256) { res.W = ReadDWord(stm, ix + 8); } ix += 12; } ix = ReadDWord(stm, ix); if(ix == 0) break; } } return res; } public static void Main(string[] args) { HWDim hw = TiffDim.Find(@"C:\test.tif"); Console.WriteLine(hw.H + " " + hw.W); } } }