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

时间:2013-03-12 17:38:39

标签: c# image random numbers jpeg

我正在制作一个应用程序,其中随机抛出4个chits。我完成了通过单击按钮生成随机数,随机数生成在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个图像,我必须随机显示。我的问题是如何以及在何处显示它们。

5 个答案:

答案 0 :(得分:3)

完成使用此方法

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;"

答案 1 :(得分:1)

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

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

代码很少,不需要'if'或'switch'。

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

答案 2 :(得分:0)

怎么样:

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));
}

答案 3 :(得分:0)

Random rand = new Random();                 int randomPosition = rand.Next(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;

            }

答案 4 :(得分:-1)

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)];
}