1>最初方案:vim
用wpf的image控件循環顯示圖片,達到動畫效果,其實就是在後臺代碼動態改變Image.Source的值,關鍵代碼:數組
for (int i = 1; i < 601; i++)//六百張圖片 { BitmapImage bmImg = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + i.ToString() + ".png")); bmImg.CacheOption = BitmapCacheOption.None; vimage..Source=bmImg.Clone(); System.Threading.Thread.Sleep(40);//每秒25幀 }
因爲Image.Source切換BitmapImage後,仍然抓着舊的bmImg不放,致使內存溢出.再多的內存也不夠用.緩存
在網上也找了你們說的方案都不行.只能尋找其餘方案.動畫
2>最終方案:spa
思路:a>先把全部要加載的圖片轉換成二進制數組,再把數組緩存到List中;code
b>循環List,把二進制圖片轉換成MemoryStream;blog
c>把MemoryStream,經過ImageSourceConverter.ConvertFrom() as BitmapFrame,進行轉換.圖片
優勢:先把圖片緩存成二進制,這樣能夠釋放對圖片文件資源的佔用,後面代碼執行效率高;用經過MemoryStream生成的Source,用完就被釋放了[暫時解釋不了]!內存
核心代碼:資源
imageSourceConverter = new ImageSourceConverter(); byList = new List<byte[]>(); for (int i = 1; i < 601; i++) { using (BinaryReader binReader = new BinaryReader(File.Open(AppDomain.CurrentDomain.BaseDirectory + i.ToString() + ".png", FileMode.Open))) { FileInfo fileInfo = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + i.ToString() + ".png"); //byte[] bytes = binReader.ReadBytes((int)fileInfo.Length); //byList.Add(bytes); if ((long)int.MaxValue > fileInfo.Length) { byte[] bytes = binReader.ReadBytes((int)fileInfo.Length); byList.Add(bytes); } else { int leng = 1024; byte[] bytes = new byte[fileInfo.Length]; for (long j = 0; j < (fileInfo.Length / (long)leng + (long)1); j++) { byte[] b = binReader.ReadBytes(leng); if (b == null || b.Length < 1) { break; } for (long jj = j * leng; jj < (j + 1) * leng; jj++) { bytes[jj] = b[jj % leng]; } } byList.Add(bytes); } } }// // //////////////
真誠期待你們指導...