Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting image size without locking the file in WPF

Tags:

c#

image

wpf

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?

like image 228
Gabe Miller Avatar asked Oct 09 '22 13:10

Gabe Miller


1 Answers

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); 
}
like image 54
Stecya Avatar answered Oct 13 '22 11:10

Stecya