Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 568
vdh_ant Avatar asked Apr 24 '09 06:04

vdh_ant


2 Answers

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;
like image 94
Kent Boogaart Avatar answered Nov 17 '22 08:11

Kent Boogaart


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;
}
like image 22
Charlie Avatar answered Nov 17 '22 09:11

Charlie