If you just need to read the width and height of a file without loading it into memory, you can use the WPF BitmapDecoder class:
using System; using System.IO; using System.Linq; using System.Windows.Media.Imaging; class Program { static void Main() { using (var stream = File.OpenRead(@"c:\work\some_huge_image.tif")) { var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None); var frame = decoder.Frames.First(); Console.WriteLine( "width: {0}, height: {1}", frame.PixelWidth, frame.PixelHeight ); } } }
If for some reason you are stuck in some era of pre- .NET 3.0, you can look at the image metadata to extract this information without loading the entire image in memory, as shown in the following answer .
source share