如何交换图片

时间:2014-04-17 14:56:33

标签: c# arrays winforms timer picturebox

嗨我有一个问题如何在我的图片框中交换图片 EX:pictuebox1显示pic1然后pictuebox 2显示pic2 然后他们交换照片然后picturebox1显示pic2和picturebox2显示pic3然后他们再次交换。我怎么做?如果有人能帮助我,它会打破我的头脑,我会非常感激。

    string[] picture = {
                       @"ImageResource\Die_Images\die1.jpg",
                       @"ImageResource\Die_Images\die2.jpg",
                         @"ImageResource\Die_Images\die3.jpg",
                           @"ImageResource\Die_Images\die4.jpg",
                             @"ImageResource\Die_Images\die5.jpg",
                                @"ImageResource\Die_Images\die6.jpg"};
    int index = 0;
    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        index++;
        if (index >= picture.Length)
            index = 1;
        else

            pictureBox1.ImageLocation = string.Format(@"ImageResource\Die_Images\die{0}.jpg", index);
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            if (index >= picture.Length)
                index = 0;
            else
            pictureBox2.ImageLocation = string.Format(@"ImageResource\Die_Images\die{0}.jpg", index);
            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;


    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Stop();
    }

1 个答案:

答案 0 :(得分:0)

不确定这是你在看什么,但试一试。

private int operationState = 0; 

private void timer1_Tick(object sender, EventArgs e)
{
    switch(operationState)
    {
        case 0://Next image
        {
            pictureBox1.ImageLocation = picture[index];
            pictureBox2.ImageLocation = picture[index + 1];

            index++;
            if (index >= (picture.Length - 1))
            {
                index = 0;
            }
            break;
        }
        case 1://Swap
        {
            string tempLocation =  pictureBox1.ImageLocation;
            pictureBox1.ImageLocation = pictureBox2.ImageLocation;
            pictureBox2.ImageLocation = tempLocation;
            break;
        }
    }

    operationState = (++operationState) % 2;
}

我从Tick事件中删除了SizeMode设置代码,您只能在其他地方执行一次。