picturebox 图像随机化 C#

发布于 2024-09-01 23:16:08 字数 1317 浏览 5 评论 0原文

我正在开发一个拼图滑块程序,并尝试随机化图片盒内的图像。我在互联网上做了一些研究,找不到任何我可以研究的例子。这些是我的代码:

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2024-09-08 23:16:14

只需填充数组并使用 shuffle 算法即可。

也许作为扩展方法实现:

namespace ExtensionMethods
{
    public static class Extensions
    {
        static Random rng = new Random();

        public static void shuffle<T>(this T[] array)
        {
            // i is the number of items remaining to be shuffled.
            for (int i = array.Length; i > 1; i--)
            {
                // Pick a random element to swap with the i-th element.
                int j = rng.Next(i);  // 0 <= j <= i-1 (0-based array)
                // Swap array elements.
                T tmp = array[j];
                array[j] = array[i - 1];
                array[i - 1] = tmp;
            }
        }

    }
}

调用示例:

使用 ExtensionMethods;

namespace ConsoleApplication
{

    static class Program
    {
        static void Main()
        {
            int[] array = new int[] {1,2,3,4,5,6,7,8,9};

            array.Shuffle();
        }
    }
}

Simply fill the array and use a shuffle algorithm.

Perhaps implement as an extension method:

namespace ExtensionMethods
{
    public static class Extensions
    {
        static Random rng = new Random();

        public static void shuffle<T>(this T[] array)
        {
            // i is the number of items remaining to be shuffled.
            for (int i = array.Length; i > 1; i--)
            {
                // Pick a random element to swap with the i-th element.
                int j = rng.Next(i);  // 0 <= j <= i-1 (0-based array)
                // Swap array elements.
                T tmp = array[j];
                array[j] = array[i - 1];
                array[i - 1] = tmp;
            }
        }

    }
}

Calling sample:

using ExtensionMethods;

namespace ConsoleApplication
{

    static class Program
    {
        static void Main()
        {
            int[] array = new int[] {1,2,3,4,5,6,7,8,9};

            array.Shuffle();
        }
    }
}
苹果你个爱泡泡 2024-09-08 23:16:13

创建一个与图片数组大小相等的布尔数组,

bool[] usedPictures = new bool[pictures.Length];

并将该数组的值设置为 false。现在确定您的随机数,并测试该元素是否被使用,例如:

int iCount = 0;
Random random = new Random();
while (iCount < pictures.Length)
{
    int attempt = random.Next(0, pictures.Length);

    //Ensures you will only use an available picture
    if (usedPictures[attempt] == false)
    {            
        picBox[attempt].Image= pictures[iCount];
        doorUsed[attempt] = true;
        iCount++;
    }
}

Create an array of bools equal to the size of the pictures array

bool[] usedPictures = new bool[pictures.Length];

Set the values of this array to false. Now determine your random number, and test if that element is used or not, something like:

int iCount = 0;
Random random = new Random();
while (iCount < pictures.Length)
{
    int attempt = random.Next(0, pictures.Length);

    //Ensures you will only use an available picture
    if (usedPictures[attempt] == false)
    {            
        picBox[attempt].Image= pictures[iCount];
        doorUsed[attempt] = true;
        iCount++;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文