获取图像大小而不在WPF中锁定文件

时间:2011-11-06 11:33:47

标签: c# wpf image

在WPF应用程序中,我在真正加载它之前得到图像大小(宽度和高度)(因为我正在加载它以减小大小......)并且我使用这个C#代码来获取它:

BitmapFrame frame = BitmapFrame.Create(new Uri(path), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
Size s = new Size(frame.PixelWidth, frame.PixelHeight);

工作正常,但它锁定了我以后想要由应用程序删除的图像文件但不能。我知道,如果我设置BitmapCacheOption.OnLoad它解决了问题,但它加载了图像,所以我失去了我想要加载缩小尺寸(使用DecodePixelWidth等)的优势。

所以任何人都知道如何预先获取图像大小而不锁定图像?

1 个答案:

答案 0 :(得分:4)

在获得图像大小后,也许你应该使用块来使用块来移除锁定

using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
   BitmapFrame frame = BitmapFrame.Create(fileStream , BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
   Size s = new Size(frame.PixelWidth, frame.PixelHeight); 
}