2 thoughts on “Using WPF Imaging classes – Getting image dimensions without reading the entire file

  1. user

    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;
    
    Reply
  2. user

    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;
    }
    
    Reply

Leave a Reply

Your email address will not be published.