【问题标题】:Loop multiple images through multiple pictureboxes通过多个图片框循环多个图像
【发布时间】:2016-09-03 16:32:21
【问题描述】:

我想同时显示四个图像,并在表单加载时图像切换位置。目前,图像会以不同的数量出现,例如:将出现 1 张图像或 2 张图像,依此类推 4. 我还想确保不会出现重复。

Form1_Load 中的代码:

PictureBox[] boxes = new PictureBox[4];

boxes[0] = pictureBox0;
boxes[1] = pictureBox1;
boxes[2] = pictureBox2;
boxes[3] = pictureBox3;

for (int i = 0; i < boxes.Length; i++)
{
    int switcher = r.Next(0, 5);

    switch (switcher)
    {
        case 0:
            { boxes[i].Image = Properties.Resources.dog0; } break;
        case 1:
            { boxes[i].Image = Properties.Resources.dog1; } break;
        case 2:
            { boxes[i].Image = Properties.Resources.dog2; } break;
        case 3:
            { boxes[i].Image = Properties.Resources.dog3; } break;
    }
}

上面给出了两个关于当前情况的示例。

更新 - 工作

程序现在在加载时移动图像并且没有重复:)

List<Bitmap> resources = new List<Bitmap>();
resources.Add(Properties.Resources.dog0);
resources.Add(Properties.Resources.dog1);
resources.Add(Properties.Resources.dog2);
resources.Add(Properties.Resources.dog3);

resources = resources.OrderBy(a => Guid.NewGuid()).ToList();

for (int i = 0; i < resources.Count; i++)
{
    pictureBox0.Image = resources[0];
    pictureBox1.Image = resources[1];
    pictureBox2.Image = resources[2];
    pictureBox3.Image = resources[3];
}

上面给出的两个示例显示了它现在工作的情况。

【问题讨论】:

  • 创建图像数组并随机播放。遍历该数组。 stackoverflow.com/a/26931594/4767498
  • 再检查一个answer :)
  • 更新中的 for 循环绝对没有必要。它只是将同一资源指向图片框四次。

标签: c# image loops picturebox shuffle


【解决方案1】:

实现非常简单。首先,您需要对数组进行洗牌,然后对其进行迭代。 Fisher–Yates shuffle.

创建一个方法ShuffleImages如下:

public void ShuffleImages(PictureBox[] img)
{
    Random r = new Random();
    for (int i = 0; i < img.Length - 1; i++)
    {
        int j = r.Next(i, img.Length);
        PictureBox temp = img[j];
        img[j] = img[i];
        img[i] = temp;                
    }
}

并在您的Form1_Load 事件中调用该方法:

private void Form1_Load(object sender, EventArgs e)
{
    PictureBox[] boxes = new PictureBox[4];
    boxes[0] = pictureBox0;
    boxes[1] = pictureBox1;
    boxes[2] = pictureBox2;
    boxes[3] = pictureBox3;

    ShuffleImages(boxes); //call the method

    for (int i = 0; i <= 3; i++)
    {
        switch (i)
        {
            case 0:
                {  boxes[i].Image = Properties.Resources.dog0;  }
                break;
            case 1:
                {  boxes[i].Image = Properties.Resources.dog1;  }
                break;
            case 2:
                {  boxes[i].Image = Properties.Resources.dog2;  }
                break;
            case 3:
                {  boxes[i].Image = Properties.Resources.dog3;  }
                break;
        }
    }
}

                         

【讨论】:

    【解决方案2】:

    正如 M.kazem Ahkhary 指出的,您需要随机播放图像:

    List<Bitmap> resources = new List<Bitmap>();
    resources.Add(Properties.Resources.dog0);
    resources.Add(Properties.Resources.dog1);
    resources.Add(Properties.Resources.dog2);
    resources.Add(Properties.Resources.dog3);
    
    resources = resources.OrderBy(a => Guid.NewGuid()).ToList(); // Dirty but effective shuffle method
    
    pictureBox0.Image = resources[0];
    pictureBox1.Image = resources[1];
    pictureBox2.Image = resources[2];
    pictureBox3.Image = resources[3];
    

    【讨论】:

    • 谢谢,我该如何实现呢?我要换什么吗?
    • 做了一些修改,并不完全正确。您通常可以将此代码粘贴到您提供的代码上
    • 它给了我一个来自每个“resources.Add”的错误,关于无法将位图转换为字符串,另一个错误是无法隐式转换类型'System.Linq.IOrderedEnumerable ' 到 'System.Collections.Generic.List'。存在显式转换(您是否缺少演员表?)并且当前上下文中不存在“资源”。
    • 我更新了答案。资源列表必须是我认为的位图列表,并且 orderdEnumerable 必须转换为列表。最后,指定 Image 属性是资源而不是资源。
    • @GeckoIT note 可能 NewGuid 比 Random.Next() 更“随机”:对已经“更有序”的列表进行排序更快。无论如何,两者都可以,我更喜欢使用 Random
    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 2015-04-12
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多