picturebox 图像随机化 C#
我正在开发一个拼图滑块程序,并尝试随机化图片盒内的图像。我在互联网上做了一些研究,找不到任何我可以研究的例子。这些是我的代码:
Random r = new Random();
PictureBox[] picBox = new PictureBox[9];
picBox[0] = new PictureBox();
picBox[1] = new PictureBox();
picBox[2] = new PictureBox();
picBox[3] = new PictureBox();
picBox[4] = new PictureBox();
picBox[5] = new PictureBox();
picBox[6] = new PictureBox();
picBox[7] = new PictureBox();
picBox[8] = new PictureBox();
我也有位图数组:
Bitmap[] pictures = new Bitmap[9];
pictures[0] = new Bitmap(@"1.1Bright.jpg");
pictures[1] = new Bitmap(@"1.2Bright.jpg");
pictures[2] = new Bitmap(@"1.3Bright.jpg");
pictures[3] = new Bitmap(@"2.1Bright.jpg");
pictures[4] = new Bitmap(@"2.2Bright.jpg");
pictures[5] = new Bitmap(@"2.3Bright.jpg");
pictures[6] = new Bitmap(@"3.1Bright.jpg");
pictures[7] = new Bitmap(@"3.2Bright.jpg");
pictures[8] = new Bitmap(@"3.3Dark.jpg");
我尝试了几种方法,但我不知道如何将随机图片[]设置到picBox[]中:
for(int i=0; i<=8;i++)
{
picBox[i].Image= pictures[r.Next(0,9)];
}
这里的问题是一些图片框,例如picBox[1]和picBox[ 6]是重复的图片。我如何使它们不重复?非常感谢示例。
i am working on a puzzle slider program and trying to randomize images inside of pictureboxes. I did some research on the internet can't find any examples i could work on. These are my code:
Random r = new Random();
PictureBox[] picBox = new PictureBox[9];
picBox[0] = new PictureBox();
picBox[1] = new PictureBox();
picBox[2] = new PictureBox();
picBox[3] = new PictureBox();
picBox[4] = new PictureBox();
picBox[5] = new PictureBox();
picBox[6] = new PictureBox();
picBox[7] = new PictureBox();
picBox[8] = new PictureBox();
i have bitmap array too:
Bitmap[] pictures = new Bitmap[9];
pictures[0] = new Bitmap(@"1.1Bright.jpg");
pictures[1] = new Bitmap(@"1.2Bright.jpg");
pictures[2] = new Bitmap(@"1.3Bright.jpg");
pictures[3] = new Bitmap(@"2.1Bright.jpg");
pictures[4] = new Bitmap(@"2.2Bright.jpg");
pictures[5] = new Bitmap(@"2.3Bright.jpg");
pictures[6] = new Bitmap(@"3.1Bright.jpg");
pictures[7] = new Bitmap(@"3.2Bright.jpg");
pictures[8] = new Bitmap(@"3.3Dark.jpg");
i tried a few ways but i don't know how to set random pictures[] into the picBox[]:
for(int i=0; i<=8;i++)
{
picBox[i].Image= pictures[r.Next(0,9)];
}
the problem here is that some pictureboxes e.g picBox[1] and picBox[6] are repeated pictures. How do i make them non repeats? Examples are greatly appreciated thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需填充数组并使用 shuffle 算法即可。
也许作为扩展方法实现:
调用示例:
使用 ExtensionMethods;
Simply fill the array and use a shuffle algorithm.
Perhaps implement as an extension method:
Calling sample:
using ExtensionMethods;
创建一个与图片数组大小相等的布尔数组,
并将该数组的值设置为
false
。现在确定您的随机数,并测试该元素是否被使用,例如:Create an array of bools equal to the size of the pictures array
Set the values of this array to
false
. Now determine your random number, and test if that element is used or not, something like: