C#中的图像滑块

本文关键字:图像 | 更新日期: 2023-09-27 18:25:58

我想创建一个简单的图像幻灯片,当计时器切换时,它将切换到图片框的下一个索引(并将循环),但具有渐变效果。如何在C#中完成?

当前代码不切换图像?还有,我该如何创建渐变效果?

我创建了一个间隔5000毫秒的简单计时器,并在启动时启用。

 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;
        }
    }

C#中的图像滑块

您不能以这种方式比较从资源加载的位图。每次从资源中获取图像时(在使用属性InnovationX.Properties.Resources._1的情况下),都会获得位图类的新实例。比较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 
};