WPF 不刷新图像(缓存问题)
我在应用程序中动态加载图像时遇到了一个大问题。当我启动应用程序时,占位符图像源为空。当我单击按钮时,将创建图像并将其作为占位符源加载。当我再次单击时,会创建一个新图像,但会显示旧图像。图像的创建非常完美。我磁盘上的文件是它们应该的文件。
下面的函数是设置图像占位符的来源。
public void setImage(string path)
{
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(path, UriKind.Relative);
img.EndInit();
//Set Refreshing Options
img.CacheOption = BitmapCacheOption.None;
img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
placeholder.Source = img;
}
BitmapCacheOption 和 BitmapCreateOptions 这两个选项不会改变任何内容。
你们有人能帮我吗?
I have a big problem with loading an image dynamically in my application. When i start the application the placeholder image source is empty. when i click on a button an image is created and loaded as the placeholder source. when i click again a new image is created but the old image is shown. The creation of the image works perfect. The files on my disk are the files they should be.
The following function is setting the source of the image placeholder.
public void setImage(string path)
{
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(path, UriKind.Relative);
img.EndInit();
//Set Refreshing Options
img.CacheOption = BitmapCacheOption.None;
img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
placeholder.Source = img;
}
The two options BitmapCacheOption and the BitmapCreateOptions dont change anything.
Can anybody of you help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
出于性能原因,WPF 在内部缓存图像。如果您两次使用相同的 Uri,并且希望每次都获得不同的图像(例如,如果 Uri 位于返回随机图像的 Web 服务器上),那么此缓存对您来说将是一个问题。
您可能需要创建一个 WebRequest 并手动下载图像,而不是依赖 Image 类来为您完成此操作。
另一种选择是以一种简单的方式更改 Uri,使其独一无二。例如,您可以附加 GUID 作为查询字符串。
WPF caches images internally, for performance reasons. If you're using the same Uri both times, and expecting to get a different image each time (for example, if the Uri is on a Web server that returns a random image), then this cache will be a problem for you.
You'll probably have to create a WebRequest and manually download the image, rather than relying on the Image class to do it for you.
Another option would be to change the Uri in a trivial way that makes it unique. For example, you could append a GUID as a query string.