【问题标题】:Randomizing images in picture boxes without repeating随机化图片框中的图像而不重复
【发布时间】:2015-11-24 19:23:42
【问题描述】:

我是编程领域的新手,我希望能够使用一个按钮在多个图片框中显示随机照片。问题是我不希望一张照片显示在多个框中。所以每个图片框都应该包含不同的图像。在过去的几个小时里,我一直在谷歌上搜索,但我无法获得任何有用的信息。我现在所拥有的,当按下按钮时,每个图片框都变为空白。虽然这是我在 button1_Click 中的代码:

{
        List<string> name = new List<string>();
        name.Add("0.jpg");
        name.Add("1.jpg");
        name.Add("2.jpg");
        name.Add("3.png");
        List<PictureBox> box = new List<PictureBox>();
        box.Add(pictureBox1);
        box.Add(pictureBox2);
        box.Add(pictureBox3);
        box.Add(pictureBox4);
        a = 4;
        ResourceManager rm = MatchGame.Properties.Resources.ResourceManager;
        for (int i = 0; i < box.Count; i++)
        {
            int randomPic = new Random().Next(0, name.Count);
            string randomName = name[randomPic];
            name.Remove(randomName);
            Image img = rm.GetObject(randomName) as Image;
            box[i].Image = img;`

            }
}

【问题讨论】:

  • 图片是否保存在resourcemanager rm中?他们有这些名字吗?因为...我认为 GetObject 正在返回 null...
  • GetObject 方法是对的。您应该尝试调试以查看 ResourceManager 对象在运行时是否有您的图像。

标签: c# image random picturebox


【解决方案1】:

一个简单的方法是简单地随机排列你的名单

List<string> name = new List<string>();
name.Add("0.jpg");
name.Add("1.jpg");
name.Add("2.jpg");
name.Add("3.png");

List<PictureBox> box = new List<PictureBox>();
box.Add(pictureBox1);
box.Add(pictureBox2);
box.Add(pictureBox3);
box.Add(pictureBox4);

// 2 lines code for shuffle every kind of IEnumerable
Random r = new Random();
name = name.OrderBy(x => r.Next()).ToList();

ResourceManager rm = MatchGame.Properties.Resources.ResourceManager;
for (int i = 0; i < box.Count; i++)
{
// no need to remove elements from name list
    string randomName = name[i];
    Image img = rm.GetObject(randomName) as Image;
    box[i].Image = img;`
}

这将确保每张图片只选择一次(当然,只要图片框的数量与存储在资源中的图像相同)。

确保每个 rm.GetObject 返回不同的图像。

附带说明,切勿在循环中创建new Random():实例化单个Random 并继续在其上调用.Next(请参阅this question)。上面的代码这样会出错:

name = name.OrderBy(x => new Random.Next()).ToList();

【讨论】:

  • 欢迎您@AndreiUrsu。如果您认为它对您有用,请将答案标记为已接受
【解决方案2】:

您可以做的是将图片参考存储在字典中。 将图片名称与 PictureBox 索引相关联 - 然后您需要做的就是检查字典值并查看图片名称是否在字典中。如果它在字典中 - 然后让 while 循环执行另一个循环 - 选择另一个图像。要回收,您需要做的就是清除字典,然后该过程可以重新开始。

Dictionary<int, string> MyActivePictures = new Dictionary<int, string>();

如果您是多线程,请使用 concurrentDictionary。

    // Where MyActivePictures < PictureBoxControl , picturename > is the Dictionary

    Dictionary<int, string> MyActivePictures = new Dictionary<int, string>();
  //  i  is your PictureBoxes index as you loop through them
    int i = 0;
    if(box.Count < name.Length){
    do
    {
        int randomPic = new Random().Next(0, name.Count);
        string randomName = name[randomPic];
        if(!MyActivePictures.Values.Contains[randomName])
        {
            name.Remove(randomName);
            Image img = rm.GetObject(randomName) as Image;
            box[i].Image = img;
            MyActivePictures[i]=randomName;

            i++;
        }
        if (i > name.Length) // exits the loop in case there are more
        {
        i = box.Count + 1;
        }

    }while (i < box.Count);

} 我应该补充一点,如果上述代码无法获得唯一的图片 - 例如图片框 > 图像数量。那么这段代码就会陷入死循环。您将需要在 while 循环中考虑该场景 - if(图片框值的迭代器 I > 总图像 - 退出循环。所以 if(i > totalimages) 退出循环。 我添加了额外的代码来处理这个:但是 您也可以简单地将该测试包含在 while 条件中 - 它更容易 ((i names.Length))

【讨论】:

    猜你喜欢
    • 2011-02-20
    • 1970-01-01
    • 2016-06-23
    • 2016-01-16
    • 1970-01-01
    • 2020-01-28
    • 2017-11-12
    • 1970-01-01
    • 2013-09-07
    相关资源
    最近更新 更多