How to do random image uploading?

Asked

Viewed 622 times

1

How do I randomly upload images to the background of a app, so that each time the user enters a given page the background changes between the images ?

How should I proceed to do this?

  • 1

    It will keep switching between images while on the page or whenever it enters the page will load a random background?

  • whenever it enters the page it will load a random background

1 answer

1


Create a method private on the page that will have this effect in the background, in the case of the example is called Pagina1.

private void RandomImage()
{
      Random random = new Random();
      String nameim = random.Next(1, 3).ToString() + ".jpg";
      Uri uri = new Uri("ms-appx:/Assets/" + nameim, UriKind.RelativeOrAbsolute);
      ImageBrush img = new ImageBrush();
      img.ImageSource = new BitmapImage(uri);
      this.Background = img;
}

'Cause I put in the Next(1,3) of Random?

Within the pasta Assests has two pictures with the respective names 1.jpg and 2.jpg, So in the example I only have these numbered images to create the random effect. If you have more pictures then you will have to increase the number 3 to the number of (imagens + 1) and realize that they are sequential numbers.

To work put this method RandomImage() within your Construtor below the InitializeComponent():

public Pagina1()
{
     this.InitializeComponent();
     this.RandomImage();
}

From that every time you charge to Pagina1, the background will change according to the number generated by Random.

Reference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.