wpf4.5 关于 BitmapImage设置图片后 资源占用的问题问题出现: 设计背景图后,可以删除背景图,发现图片被占用
解决办法 , 最后一步 调用 BitmapImage对象的clone方法, 不支持packuri
也可以设置
BitmapImage bitmapImage = new BitmapImage(); //初始化BitmapImage类的一个新实例Image image1 = new Image(); //定义一个Image控件string strPath = "D:\\mImage.png";//图片所在的位置bitmapImage.BeginInit(); //表示BitmapImage初始化开始bitmapImage.CacheOption = BitmapCacheOption.Onload;bitmapImage.UriSource = new Uri(strPath);//获取或设置BitmapImage的Uri源bitmapImage.EndInit();//表示BitmapImage初始化结束image1.Source = bitmapImage;//将image1控件的源指定为bitmapImage
或者
读取文件到流,然后到内存去private void InitImage()
{
using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
{
FileInfo fi = new FileInfo(filePath);
byte[] bytes = reader.ReadBytes((int)fi.Length);
reader.Close();
image = new Image();
bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = new MemoryStream(bytes);
bitmapImage.EndInit();
image.Source = bitmapImage;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
this.LayoutRoot.Children.Add(image);
}
}
推荐您阅读更多有关于“WPF4.5,”的文章