locked
Random image generator code to share? RRS feed

  • Question

  • Is anybody willing to share a code which is a random image genrator? I've tried to develop a bit: first the gadget would get a random number (of the total amount of images) and then place the number into the image URL path. Images are named as numbers in images folder.

    Something that I've done:

    ____________________

     void GenerateNumber()

    {

    Random rand = new Random();

    rand.Next(1, 10);

    Image imagran = new Image();

    imagran.Source = new BitmapImage(new Uri("WindowsPhoneRandom;component/images/" + rand + ".png", UriKind.RelativeOrAbsolute));

    ...

    }

     

    ...

    }

    ______________________________

    There should be a button that generates the whole function. I found it difficult to say to the gadget: "1) When button is pressed, 2) get a random number and 3) put the number in the URL path; 4) Show the image of the specified path above the button". 

    I wish somebody had a complete solution to share.

    Monday, September 27, 2010 11:28 AM

All replies

  • In the code above you create a new image, instead of doing that, you should get setting the .Source property of an image that already exists on screen and in your xaml code.

    So if your xaml had: <Image x:Name="MyImage" />

    Your code would do this.MyImage.Source = new Bitm...

     

    Monday, September 27, 2010 1:36 PM
    Moderator
  • In fact, I've done that too because my XAML looks like this

     

     

     

    <Image Height="70" x:Name="imagran" Stretch="Fill" Width="200"/>

    But it's about the whole thing. How to get it work...

    But thanks anyway.

    Tuesday, September 28, 2010 7:13 AM
  • Here is the code, am I missing something important?

    CS:

    ...

     public MainPage()
           {
                InitializeComponent();

                generateButton = (ApplicationBarIconButton)this.ApplicationBar.Buttons[0];

                generateButton.Click += new EventHandler(generateButton_Click);
            }


            void generateButton_Click(object sender, EventArgs e)
            {
            GeneratePic();
           
            }

    ...

    void GeneratePic()
    {
    Random rand = new Random();
    rand.Next(1, 3);

    Image imagran = new Image();
    this.imagran.Source = new BitmapImage(new Uri("images/" + rand + ".png", UriKind.RelativeOrAbsolute));
    }

    ...

     

    XAML:

    ...

    <Image Height="70" x:Name="imagran" Width="200" />

    ...

    <phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
    <shell:ApplicationBarIconButton x:Name="generateButton" IconUri="/icons/appbar.refresh.rest.png" Text="generate"></shell:ApplicationBarIconButton>
    </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>

    ...

    Tuesday, September 28, 2010 10:19 AM
  • this line -> Image imagran = new Image(); is redundant, it doesn't do anything.

    The rest of it looks correct, what I would check are the following:

    1. Does the click event handler actually get called?  (Set a breakpoint in the GeneratePic() method to be sure it actually gets run)
    2. Do the images actually exist in the correct place?  If your .xap file is in /a/b/c your images need to be in /a/b/c/images/123.png for example
    Tuesday, September 28, 2010 12:22 PM
    Moderator