Image file locked after loading in WPF

孤街浪徒 提交于 2019-12-10 14:29:09

问题


I am reading my WPF imagesource like this:

VB

Dim bmi As BitmapImage = New BitmapImage
bmi.BeginInit
bmi.CacheOption = BitmapCacheOption.None
bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache
bmi.UriSource = New Uri(input.FullName, UriKind.Absolute)
bmi.EndInit

C#

BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.CacheOption = BitmapCacheOption.None;
bmi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bmi.UriSource = new Uri(input.FullName,  UriKind.Absolute);
bmi.EndInit();

It works like it should until this point. But user can update the image by copying the file. Then I want to refresh the image. But the file "MyFileName" is locked and when I want do overwrite it, it throws an error that it is already in use and locked.

But wait, I searched here for a solution and I got it:

bmi.cachoption = OnLoad

was the key... BUT!! now, the image is always the old one and is not updated to the new file. How to clear this cache?

In VB.Net I made an System.Drawing.Bitmap from stream. How to do it best way in WPF?

Regards


回答1:


dlev had a good advice. Here you see the cache option that should solve it: Problems overwriting (re-saving) image when it was set as image source

imgTemp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;


来源:https://stackoverflow.com/questions/7122008/image-file-locked-after-loading-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!