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,”的文章
本文介绍了解决WPF中使用BitmapImage作为图片源时遇到的资源占用问题。通过具体步骤演示如何初始化BitmapImage,并展示了如何从文件加载图片到内存中,最后讨论了缓存选项对资源管理的影响。
256

被折叠的 条评论
为什么被折叠?



