2013-08-03 87 views
-1

我想創建一個簡單的圖像幻燈片,當定時器開關時,它將切換到picturebox的下一個索引(並將循環),但具有淡入淡出效果。它如何在C#中完成?C#中的圖像滑塊?

當前代碼不切換圖像?而且 - 我如何創建淡入淡出效果?

我創建了一個間隔爲5,000毫秒的簡單定時器,在啓動時啓用它。

private void timerImage_Tick(object sender, EventArgs e) 
    { 
     if (pictureBox1.Image.Equals(InnovationX.Properties.Resources._1)) 
     { 
      pictureBox1.Image = InnovationX.Properties.Resources._2; 
     } 
     else if (pictureBox1.Image.Equals(InnovationX.Properties.Resources._2)) 
     { 
      pictureBox1.Image = InnovationX.Properties.Resources._3; 
     } 
     else 
     { 
      pictureBox1.Image = InnovationX.Properties.Resources._1; 

     } 
    } 
+0

純C#或使用WPF? – Sharath

+0

@Sharath我從來沒有使用WPF,但是更容易理解。 –

+0

您可能需要使picturebox無效。我也會轉向另一種比較。 –

回答

1

您無法比較以這種方式從資源加載的位圖。每次你從資源中獲取圖像(在你的情況下使用屬性InnovationX.Properties.Resources._1),你將獲得新的Bitmap類實例。比較Bitmap類的兩個不同實例總是會導致錯誤,即使它們包含相同的圖像。

Bitmap a = InnovationX.Properties.Resources._1; // create new bitmap with image 1 
Bitmap b = InnovationX.Properties.Resources._1; // create another new bitmap with image 1 
bool areSameInstance = a == b; // will be false 

如果加載從資源圖像成員變量(例如,在加載事件)。

// load images when you create a form 
private Bitmap image1 = InnovationX.Properties.Resources._1; 
private Bitmap image2 = InnovationX.Properties.Resources._2; 
private Bitmap image3 = InnovationX.Properties.Resources._3; 

// assing and compare loaded images 
private void timerImage_Tick(object sender, EventArgs e) 
{ 
    if (pictureBox1.Image == image1) 
    { 
     pictureBox1.Image = image2; 
    } 
    else if (pictureBox1.Image == image2) 
    { 
     pictureBox1.Image = image3; 
    } 
    else 
    { 
     pictureBox1.Image = image1; 
    } 
} 

在這之後,重寫代碼使用數組:)

Image[] images = new { 
    InnovationX.Properties.Resources._1, 
    InnovationX.Properties.Resources._2, 
    InnovationX.Properties.Resources._3 
};