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

前端 未结 2 1408
暗喜
暗喜 2021-01-31 05:12

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

相关标签:
2条回答
  • 2021-01-31 05:35

    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;
    }
    
    0 讨论(0)
  • 2021-01-31 05:46

    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;
    
    0 讨论(0)
提交回复
热议问题