-1

我想创建一个简单的图像幻灯片,当定时器切换时,它将切换到图片框的下一个索引(并将循环)但具有淡入淡出效果。如何在 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;

        }
    }
4

1 回答 1

1

您不能以这种方式比较从资源加载的位图。每次从资源中获取图像时(在您的情况下使用 property 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 事件中)。

// 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 
};
于 2013-08-03T07:32:03.820 回答