gpt4 book ai didi

c# - C#显示多张图片

转载 作者:太空宇宙 更新时间:2023-11-03 19:31:05 29 4
gpt4 key购买 nike

我已经编写了代码来使用 PictureBox 显示多张图像(100 张图像),但是当运行该应用程序时,只显示了一张图像...

请大家帮帮我...

这是我的代码:

    Random r = new Random();

private int randomPoint()
{

return 1 + r.Next() % 15;

}

// x0, y0
private int[] initialLocation = new int[2];

private void setLocation(int i)
{
int x0 = 50, y0=50;

initialLocation[1] = y0;
switch (i)
{
case 1: initialLocation[0] = x0;
break;

case 2: initialLocation[0] = x0 + 50;
break;

case 3: initialLocation[0] = x0 + 100;
break;

case 4: initialLocation[0] = x0 + 150;
break;

case 5: initialLocation[0] = x0 + 200;
break;

case 6: initialLocation[0] = x0 + 250;
break;

case 7: initialLocation[0] = x0 + 300;
break;

case 8: initialLocation[0] = x0 + 350;
break;

case 9: initialLocation[0] = x0 + 400;
break;

case 10: initialLocation[0] = x0 + 450;
break;

}

}

public Form1()
{
InitializeComponent();

PictureBox[] p = new PictureBox[10];


for (int i=0; i<10;i++)
{

p[i] = new PictureBox();
p[i].ImageLocation = "1.png";

int[] l = new int[2];

// create random location for images
setLocation(randomPoint());

p[i].Location = new Point(initialLocation[0], initialLocation[1]);

this.Controls.Add(p[i]);
}
}

最佳答案

这是因为您每次需要图像时都在声明随机数生成器:

private  int randomPoint()
{
Random r = new Random();

return 1 + r.Next() % 15;
}

将其替换为:

private Random r = new Random();

private int randomPoint()
{
return 1 + r.Next() % 15;
}

更新

如果我没理解错的话,您希望在整个表单中以随机顺序显示 15 张图像。

为确保您不会得到任何重复,您需要确保在选择下一张之前从列表中删除您选择的图片。所以(在伪代码中)你需要这样的东西:

this.folderList = new List<string>();
// Populate it in an orderly manner

// Create temporary list to put the randomly selected items into
var radomisedList = new List<string>();

// Randomise the list.
var random = new Random();
int num = 0;

while (this.folderList.Count > 0)
{
num = random.Next(0, this.folderList.Count);
radomisedList.Add(this.folderList[num]);
this.folderList.RemoveAt(num);
}

这将确保您获得随机订单且不会重复。

关于c# - C#显示多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4862446/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com