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

vdh_ant picture vdh_ant · Apr 24, 2009 · Viewed 9.3k times · Source

Link this post I want to be able to read an image files height and width without reading in the whole file into memory.

In the post Frank Krueger mentions there is a way of doing this with some WPF Imaging classes. Any idea on how to do this??

Answer

Kent Boogaart picture Kent Boogaart · Apr 24, 2009

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;