Search code examples
c#imagerandomnumbersjpeg

How to Generate random images through numbers c#?


I am making an app in which 4 chits are to be thrown randomly. I am done with generating random numbers by clicking on a button random numbers are generated between 1 to 4.

My question is how do I associate the 4 images with those numbers? Suppose if 1 comes randomly - then 1.jpg should be displayed. My programming language is c# and working in 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();
    }}}

i m showing the numbers in a textblock .now i have 4 images which i have to display randomly.,my question is how and where to display them .


Solution

  • Done used this method

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