I am creating an BitmapImage from an existing image using:
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(jpegPath, UriKind.Relative);
bmp.EndInit();
After I have done this I want to delete the image from my hard drive, but it is locked. Is there a way I can unlock it so it can be removed?
bmp.CacheOption = BitmapCacheOption.OnLoad;
That will load the image into memory completely and won't leave a lock on the image file.
JPG is still set as the source of existing object in your application. Try to set the source of BitmapImage to something else or remove it completely. Or create a copy of your JPG in memory.
来源:https://stackoverflow.com/questions/5116260/c-sharp-4-0-unlock-image-after-creating-bitmapimage