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

时间:2023-02-09 15:19:43

In a WPF application I get the image size (width and height) before really loading it (as I am loading it with reduced size...) and I am using this C# code to get it:

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

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

That works fine but then it locks the image file that I later want to delete by the application but cannot. I know, if I set BitmapCacheOption.OnLoad it solves the problem but then it loads the image so I lose the advantage I want to get with loading it with reduced size (using DecodePixelWidth etc.).

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

So anyone knows how to get the image size beforehand without locking the image?

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

1 个解决方案

#1


4  

Maybe you should use stream in using block to remove lock after you get your image size

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

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

#1


4  

Maybe you should use stream in using block to remove lock after you get your image size

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

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