Using WPF Imaging classes - Getting image dimensions without reading the entire file Using WPF Imaging classes - Getting image dimensions without reading the entire file wpf wpf

Using WPF Imaging classes - Getting image dimensions without reading the entire file


This should do it:

var bitmapFrame = BitmapFrame.Create(new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg"), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);var width = bitmapFrame.PixelWidth;var height = bitmapFrame.PixelHeight;


Following Sir Juice's recommendation, here is some alternative code that avoids locking the image file:

using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)){    var bitmapFrame = BitmapFrame.Create(stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);    var width = bitmapFrame.PixelWidth;    var height = bitmapFrame.PixelHeight;}