How To Display Random Images In Windows Phone

August 13, 2012 | Rishabh Agrawal | General

I was making an aplication in which i have to display the random images as a result but I was unable to do so.

Previously, the code I was using is as follows:

private void buttonNextImage_Click(object sender, EventArgs e)

{

var rand = new Random();

var files = Directory.GetFiles(@”serverscreens”, “*.jpg”);

pictureBox1.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);

}

 

But this code has some bugs and was not working right. Finally, after lot of hours spent I came up with the solution that worked perfectly fine. The code for displaying random images in windows phone 7 is as follows:

public void OnLoaded(object sender, RoutedEventArgs e)

{// Create  struct instance and initialize it.

RandomGenratorr mygen = new RandomGenratorr()

String str = mygen.GetRandom()

img.Source = new BitmapImage(new Uri(str, UriKind.RelativeOrAbsolute));//create the image in your Xaml file and give its refernce here.//

}

//Now create the class Random Generator

class RandomGenratorr

{// Create  struct instance and initialize it.

Random rdn = new Random();

List<String> Res = new List<string>();

public RandomGenratorr()

{

Res.Add(“Path of your File”);

Res.Add(“Path of your File”);

Res.Add(“Path of your File”);

}

public string GetRandom()

{

Random random = new Random();

int rescount = Res.Count;

int ranNumber = random.Next(0, (rescount – 1));

return Res[ranNumber];

}

I hope this will help you out and save many hours that I have invested in searching a solution for this problem.

— By

Priyanka Madaan


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Comments are closed.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal