通过多个图片框循环多个图像
Loop multiple images through multiple pictureboxes
我想同时显示四张图片,并在表单加载时切换图片位置。目前,图片会以不同的数量出现,例如:会出现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];
}
上面给出的两个例子展示了现在它工作时会发生什么。
正如 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];
实现非常简单。首先,您需要打乱数组,然后遍历它。 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;
}
}
}
我想同时显示四张图片,并在表单加载时切换图片位置。目前,图片会以不同的数量出现,例如:会出现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];
}
上面给出的两个例子展示了现在它工作时会发生什么。
正如 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];
实现非常简单。首先,您需要打乱数组,然后遍历它。 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;
}
}
}