WPF 冻结位图图像不显示
我正在使用绑定到 UI 上的图像属性的视图模型,并且视图模型包含 ImageSource 属性。我使用以下函数设置该属性
private BitmapImage GetImageFromUri(Uri urisource)
{
if (urisource == null)
return null;
var image = new BitmapImage();
image.BeginInit();
image.UriSource = urisource;
image.EndInit();
image.Freeze(); //commenting this shows the image if the routine is called from the proper thread.
return image;
}
出于某种奇怪的原因,在以下代码中,当我对 BitmapImage 调用 Freeze 时,它不会出现在主窗口上。我没有遇到异常或崩溃。有人能帮我解决这个问题吗? 我正在异步设置图像属性,因此我需要能够使用创建的图像,假设 GetImageFromUri 调用是从 UI 线程以外的线程进行的。
I am using a viewmodel bound to an image property on the UI and the viewmodel contains an ImageSource property. I set that property using the following function
private BitmapImage GetImageFromUri(Uri urisource)
{
if (urisource == null)
return null;
var image = new BitmapImage();
image.BeginInit();
image.UriSource = urisource;
image.EndInit();
image.Freeze(); //commenting this shows the image if the routine is called from the proper thread.
return image;
}
For some odd reason, in the following code, when I call Freeze on my BitmapImage, it does not appear on the main window.I get no exception or crash. Can anybody help me with this?
I am setting the image property asynchronously so I need to be able to use the created image, assuming the GetImageFromUri call was made from a thread other than the UI thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我使用
StreamSource
,对我来说就足够了:但是如果我使用
UriSource
我需要:BitmapSource
有UriSource
,但它在 xaml 渲染后起作用IF i use
StreamSource
, for me is enough:But if i use
UriSource
i need:BitmapSource
hasUriSource
, but it works after xaml rendered在冻结 BitmapImage 之前尝试设置 CacheOption。看看这是否有效 -
Try setting the CacheOption for BitmapImage before freezing it. See if this works -
在冻结它之前,您需要完全渲染它。
您应该尝试侦听 SourceUpdated 事件,然后才冻结图像。
顺便说一句,如果您想在此之后修改图像,则必须克隆它。
Before you freeze it, you need to fully render it.
You should try to listen to the SourceUpdated event, and only then freeze the image.
On a side note, if you ever want to modify the Image after that, you will have to Clone it.