WPF调用图片过程中,最大问题就是图片占用,用这个方法将图片读取方式改为放到内存中,可以有效避免图片占用.
string imgpath="D:\\files..";
BitmapImage bitmap = new BitmapImage();
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(imgpath)))
{
bitmap = new BitmapImage();
bitmap.DecodePixelHeight = 100;
bitmap.BeginInit();
bitmap.CacheOption = BitmapCacheOption.OnLoad;//设置缓存模式
bitmap.StreamSource = ms;//通过StreamSource加载图片
bitmap.EndInit();
bitmap.Freeze();
}
Image img = new Image
{
Height = 98,
Source = bitmap
};