使用WPF Imaging类 - 获取图像尺寸而不读取整个文件

时间:2009-04-24 06:23:36

标签: wpf image image-processing file-io metadata

链接this帖子我希望能够在不将整个文件读入内存的情况下读取图像文件的高度和宽度。

在Frank Krueger的帖子中提到,有一种方法可以通过一些WPF Imaging类来实现。关于如何做到这一点的任何想法??

2 个答案:

答案 0 :(得分:44)

这应该这样做:

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;

答案 1 :(得分:19)

按照Juice先生的建议,这里有一些替代代码可以避免锁定图像文件:

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;
}
相关问题