BitmapImage in WPF does lock file

女生的网名这么多〃 提交于 2019-11-26 07:50:03

问题


I use:

Dim bmi As New BitmapImage(New Uri(fiInfo.FullName, UriKind.Absolute))
bmi.CacheOption = BitmapCacheOption.OnLoad

this does not Use OnLoad And file still is locked to overwrite on harddisk. Any idea how to unlock?

Regards


回答1:


As shown in the question you link to, you'd need to call BeginInit and EndInit, like so as well as set the UriSource property:

Dim bmi As New BitmapImage()
bmi.BeginInit()
bmi.CacheOption = BitmapCacheOption.OnLoad
bmi.UriSource = New Uri(fiInfo.FullName, UriKind.Absolute)
bmi.EndInit()



回答2:


Read the BitmapImage from file and rewrite it with a MemoryStream:

MemoryStream ms = new MemoryStream();
BitmapImage bi = new BitmapImage();
byte[] bytArray = File.ReadAllBytes(@"test.jpg");
ms.Write(bytArray, 0, bytArray.Length);ms.Position = 0;
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
image.Source = bi;



回答3:


I had a similar problem and I solved using this method: (it's a personalization of an answer here)

    public static BitmapImage BitmapFromUri(Uri source)
    {
        var bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.UriSource = source;
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.EndInit();
        return bitmap;
    }

You can open the image like this:

BitmapImage bimg = BitmapFromUri(new Uri(some_URI));

And it releases the image immediatly after loading it.

Hope it can helps!




回答4:


BitmapFrame.Create(new Uri(filePath), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);


来源:https://stackoverflow.com/questions/6430299/bitmapimage-in-wpf-does-lock-file

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