如何通过数字生成随机图像

本文关键字:随机 图像 何通过 数字 | 更新日期: 2023-09-27 18:03:28

我正在制作一个应用程序,其中4个chit将被随机抛出。我完成了通过单击按钮生成随机数,随机数在1到4之间生成。

我的问题是我如何将4个图像与这些数字联系起来?假设1是随机出现的,那么应该显示1.jpg。我的编程语言是c#,在Visual studio 2012中工作。

public sealed partial class MainPage : Page
{
   Random rand = new Random();
 public MainPage()
    {
        this.InitializeComponent();
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        int num=rand.Next(1,5);
        textblock1.Text=num.ToString();
    }}}

我在一个文本块中显示数字。现在我有4个图像,我必须随机显示。

如何通过数字生成随机图像

已使用此方法

private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Random rand = new Random();
        int dice = rand.Next(1, 5);
        switch (dice)
        {
            case 1:
                Image1.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/1.png"));
                break;
            case 2:
                two.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/2.png"));
                break;
            case 3:
                three.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/3.png"));
                break;
            case 4:
                four.Source = new BitmapImage(new Uri(@"ms-appx:///Assets/4.png"));
                break;
        }
    }for bitmap image refrence will be "using Windows.UI.Xaml.Media.Imaging;"

您可以将生成的数字替换为文件名,如:

string.Format("{0}.jpg", generatedNumber);

非常少的代码,不需要'if'或'switch'。

我还将创建一个单独的组件(I)FileNameResolver,它包含确定正确文件名和错误处理的逻辑;在你的代码中使用它

怎么样:

Random rand = new Random();
Bitmap GetRandomImage(){
    Bitmap bmp;
    int n = rand.Next(0, 4);
    if(n == 0)
       bmp = new Bitmap("mypath1");
    else if(n == 1)
       bmp = new Bitmap("mypath2");
    else if(n == 2)
       bmp = new Bitmap("mypath3");
    else
       bmp = new Bitmap("mypath4");
    return bmp;
}

或者你可以重命名你的文件,使用如下:

Random rand = new Random();
Bitmap GetRandomImage(){
    int n = rand.Next(0, 4);
    return new Bitmap(string.Format("mypath''{0}.jpg", n));
}

Random = new Random();int randomPosition = rand。下一个(0,9);

            lblEnglish.Text = sw.EnglishHeartSentence[randomPosition];
            lblShona.Text = sw.shonaSentence[randomPosition];
            switch (randomPosition)
            {
                case 0:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.diarrhea_900x600px;
                    break;
                case 1:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.hunger1; ;
                    break;
                case 2:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.stomachache1;
                    break;
                case 3:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.full_clipart_1;
                    break;
                case 4:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.Top_10_Foods_To_Relieve_Menstrual_Cramps_1;
                    break;
                case 5:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.preg;
                    break;
                case 6:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.belly;
                    break;
                case 7:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.a_man_with_visible_spine_holds_his_back;
                    break;
                case 8:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.bell;
                    break;
                case 9:
                    mainPicBox.Image = Funda_isizulu_.Properties.Resources.coffee_growl_905;
                    break;
            }

er,

switch (number)
{
    case 1:
        return "1.jpg";
    case 2:
        return "2.jpg";
    case 3:
        return "3.jpg";
    case 4:
        return "4.jpg";
    default:
        throw ArgumentException("number");
}

看情况而定,

如何,

private static readonly WhateverTypeMyImagesNeedToBe[] images =
    {
        new WhateverTypeMyImagesNeedToBe("1.jpg"),
        new WhateverTypeMyImagesNeedToBe("2.jpg"),
        new WhateverTypeMyImagesNeedToBe("3.jpg"),
        new WhateverTypeMyImagesNeedToBe("4.jpg")
    }
private static WhateverTypeMyImagesNeedToBe GetRandomImage()
{
    return images[new Random().Next(0, images.Length)];
}