Getting image size without file lock in WPF

In a WPF application, I get the image size (width and height) before loading it (since I load it with a reduced size ...), and I use this C # code to get it:

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

This works fine, but then it blocks the image file, which I later want to delete with the application, but cannot. I know if I installed BitmapCacheOption.OnLoad, it solves the problem, but then it loads the image, so I lose the advantage I want by downloading it with a reduced size (using DecodePixelWidth, etc.).

So, does anyone know how to get the image size in advance without blocking the image?

+4
source share
1 answer

Perhaps you should use a thread to use the block to remove the lock after getting the 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); } 
+4
source

Source: https://habr.com/ru/post/1379857/


All Articles