亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于.NET BitmapImage 內(nèi)存釋放問(wèn)題的解決方法詳解

 更新時(shí)間:2013年05月15日 12:08:42   作者:  
本篇文章是對(duì).NET BitmapImage 內(nèi)存釋放問(wèn)題的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下

網(wǎng)上查到的代碼,多數(shù)的寫法使用MemoryStream來(lái)實(shí)現(xiàn):

復(fù)制代碼 代碼如下:

new Thread(new ThreadStart(() => {
    var bitmap = new BitmapImage();
    bitmap.BeginInit();

    using (var stream = new MemoryStream(File.ReadAllBytes(...))) {
        bitmap.StreamSource = stream;
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.EndInit();
        bitmap.Freeze();

    }
    this.Dispatcher.Invoke((Action)delegate {
        Image1.Source = bitmap;

    });

})).Start();


今天問(wèn)題來(lái)了,當(dāng)我設(shè)置了DecodeWidth為100時(shí)加載1000張圖片,照理說(shuō)內(nèi)存應(yīng)該維持100×100的1000張圖片,但事實(shí)上他保留了所以原始圖片的內(nèi)存直到BitmapImage被回收時(shí)才釋放,這讓我很尷尬,換句話說(shuō)using(MemoryStream)并沒(méi)有真正按我們預(yù)期釋放MemoryStream中的Buffer,那如何才能釋放呢?
其實(shí)最簡(jiǎn)單就是直接棄用MemoryStream轉(zhuǎn)投FileStream,如下:
復(fù)制代碼 代碼如下:

using (var stream = new FileStream(path, FileMode.Open)) {
    image.BeginInit();
    image.StreamSource = stream;

    image.DecodePixelWidth = 100;

    image.CacheOption = BitmapCacheOption.OnLoad;
    image.EndInit();
    image.Freeze();
}


相關(guān)文章

最新評(píng)論