I have a canvas on my WPF form that I need to run a sequence of images at Runtime. How do I refresh the images in the canvas?
Asked
Active
Viewed 1,361 times
0
-
http://stackoverflow.com/questions/4214155/wpf-easiest-way-to-move-image-to-x-y-programmatically try this – Shoaib Shaikh Jan 17 '12 at 06:12
2 Answers
0
Any changes you make to visual elements in a WPF application will be reflected in the view. You do not need to invoke a Refresh method to make changes visible. For your application you could create a DispatcherTimer
, then either change your Image.Source
or replace your Image
on each Tick
.
For example:
<Canvas>
<Image x:Name="myImage" Source="SomeUri"/>
</Canvas>
And this code-behind:
var timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
timer.Tick += (s,e) =>
{
myImage.Source = // next image in sequence
}

ColinE
- 68,894
- 15
- 164
- 232
-
I tried it it does work however I can not delete nor replace the previous images (the one that were already displayed). Are they still opened by the canvas? why is the canvas (image) has the hook on them? I am using this code to refresh the uri: Uri newUri = new Uri(@inputA); Image.ImageSource = new BitmapImage(newUri); – user1094315 Jan 17 '12 at 20:07
-
Try canvas.Children.Clear() - if my answer has helped, please upvote or accept. – ColinE Jan 17 '12 at 20:24
-
It didn't work. Any other way I can clear or remove connection between the older images and canvas? Thanks for your help. – user1094315 Jan 17 '12 at 20:51
-
this should do what your question asks, though I'm not keen on manipulating the view directly like this. What is the problem exactly? – Yaur Jan 18 '12 at 20:55
0
ColinE's answer is helpful however we need to also be able to use filestream as follow so we can refresh or modify the images which are previously displayed:
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;
}
}
}

user1094315
- 95
- 2
- 9