BitmapImage in WPF does lock file

前端 未结 4 616
自闭症患者
自闭症患者 2020-11-27 22:34

I use:

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

this does not Use

相关标签:
4条回答
  • 2020-11-27 23:10

    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!

    0 讨论(0)
  • 2020-11-27 23:29

    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;
    
    0 讨论(0)
  • 2020-11-27 23:30

    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()
    
    0 讨论(0)
  • 2020-11-27 23:30
    BitmapFrame.Create(new Uri(filePath), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
    
    0 讨论(0)
提交回复
热议问题