我的WPF表单上有一个画布,我需要在运行时运行一系列图像。如何刷新画布中的图像?
答案 0 :(得分:0)
您对WPF应用程序中的可视元素所做的任何更改都将反映在视图中。您无需调用Refresh方法即可使更改可见。对于您的应用,您可以创建DispatcherTimer
,然后更改Image.Source
或替换Image
上的Tick
。
例如:
<Canvas>
<Image x:Name="myImage" Source="SomeUri"/>
</Canvas>
这个代码隐藏:
var timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
timer.Tick += (s,e) =>
{
myImage.Source = // next image in sequence
}
答案 1 :(得分:0)
ColinE的回答很有帮助,但我们还需要能够使用文件流,因此我们可以刷新或修改以前显示的图像:
FileInfo fileinfo = new FileInfo(MyFilePath);
if (fileinfo.Exists)
{
using (FileStream fs = System.IO.File.Open(MyFilePath,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
using (StreamReader reader = new StreamReader(fs))
{
BitmapImage bitImg = new BitmapImage();
bitImg.BeginInit();
bitImg.StreamSource = fs;
bitImg.EndInit();
Image.ImageSource = bitImg;
}
}
}