2014-10-29 47 views
0

我想在圖像WPF(Syste.Windows.Controls.Image)中顯示圖像序列。 在圖像之間我想設置一個暫停作爲一個幀率。 在XAML我有c#代碼中的圖像序列圖像wpf

<Image x:Name="image" HorizontalAlignment="Left" Height="100" Margin="235,145,0,0" VerticalAlignment="Top" Width="100"/> 

我想在一個週期內插入,但我爲什麼顯示ED停止(fps)的速度呢?

我已經tryed這一點,但不顯示圖像,如果不是最後的,我不喜歡線程暫停

for (int i = 2; i < 5; i++) 
     { 

     this.image.Source = new BitmapImage(new Uri("C:\\Users\\Pictures\\Braccio" + i + ".jpg", UriKind.Absolute)); 
     System.Threading.Thread.Sleep(5000); 

     } 
+0

我不知道,如果你可以動畫'Image.Source',但您可以創建許多圖像和運行[關鍵幀動畫(http://msdn.microsoft.com/en-us/庫/ ms742524.aspx)來改變它們的可見性,從而產生想要的效果。你有沒有考慮過創建gif呢?一旦將其分配給「Image」,它將自動播放。 – Sinatr 2014-10-29 11:29:18

+0

哦,你的錯誤顯然是在UI線程中一次完成整個工作。用戶界面不會在整個持續時間內更新,這就是爲什麼你只看到最後一幀*。您可以通過使用「DispatcherTimer」而不是「Thread.Sleep」來分割動畫。或者在單獨的'Task' /'Thread'中運行作業(使用'BackgroundWorker'),但是你需要在UI線程中調用*設置'Source'。 – Sinatr 2014-10-29 11:32:32

+0

謝謝,但我需要在時間有一個圖像,因爲我比較此圖像與一個參考圖像。我曾經想過,我可以在一張圖片中顯示一張圖片,在2ms後用另一個ecc更改此圖片...因此,在同一時間,我將展示圖片與我的流式圖片進行比較。 – luca 2014-10-29 11:45:37

回答

0

你通常會用一個定時器做到這一點,最好是DispatcherTimer,這樣:

private DispatcherTimer timer; 
private int imageIndex = 2; 

public MainWindow() 
{ 
    InitializeComponent(); 

    timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(5) }; 
    timer.Tick += TimerTick; 
    timer.Start(); 
} 

private void TimerTick(object sender, EventArgs e) 
{ 
    image.Source = new BitmapImage(
     new Uri(@"C:\Users\Pictures\Braccio" + imageIndex + ".jpg")); 

    if (++imageIndex == 5) 
    { 
     imageIndex = 2; 
    } 
} 
+0

謝謝,abd如果我想顯示1或N次這個序列? – luca 2014-10-29 14:15:27

+0

只要你想調用'timer.Stop()'。 – Clemens 2014-10-29 14:18:15

+0

完美,感謝您的幫助 – luca 2014-10-29 14:59:49