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

前端 未结 2 1411
暗喜
暗喜 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;
    }
    

提交回复
热议问题