c# – WPF BitmapSource ImageSource
发布时间:2020-12-15 08:09:09 所属栏目:百科 来源:网络整理
导读:我将 Image.Source属性绑定到下面显示的属性的结果. public BitmapSource MyImageSource{ get { BitmapSource source = null; PngBitmapDecoder decoder; using (var stream = new FileStream(@"C:Templogo.png",FileMode.Open,FileAccess.Read,FileShare.
我将
Image.Source属性绑定到下面显示的属性的结果.
public BitmapSource MyImageSource { get { BitmapSource source = null; PngBitmapDecoder decoder; using (var stream = new FileStream(@"C:Templogo.png",FileMode.Open,FileAccess.Read,FileShare.Read)) { decoder = new PngBitmapDecoder(stream,BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.None); if (decoder.Frames != null && decoder.Frames.Count > 0) source = decoder.Frames[0]; } return source; } } 由于某种原因,在渲染图像期间(在PresentationCore程序集中为Deep)失败.我确信图像没有损坏,因为我可以成功显示没有绑定的相同图像 <Image Name="FooImage" Source="/logo.png" /> 我必须在代码中绑定图像源,因为我最终将从base64字符串创建图像流. 任何人都知道这是否是WPF的错误?或者我做错了什么? 解决方法
问题是BitmapCacheOption选项,更改为BitmapCacheOption.OnLoad工作.
使用BitmapCacheOption.None,BitmapSource在渲染图像之前不会被解码,但是其中包含png的流已经被放置在那个点上.如果你缓存OnLoad,它会立即解码并缓存结果,而不是在流不再存在时尝试解码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |