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:
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.).
So anyone knows how to get the image size beforehand without locking the image?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With