1

I am having a problem while making a Whack a Mole type game, I am trying to create the images where the mole will appear dynamically, yet there is only a blank white screen where the stack panel is. It is fair to say that I am a noob.

This is my loop where I am trying to create these images:

        Image[] ImageArray = new Image[50];
        InitializeComponent();
        //string ImageName = "Image";
        for (int i = 0; i <= 8; i++)
        {
            Image Image = new Image();
            ImageArray[i] = Image;
            Image.Name = "Image" + i.ToString();
            StackPanel1.Children.Add(ImageArray[i]);
        }

        //Random Number Generator
        Random rnd = new Random();
        int num = rnd.Next(1, 9);

        //If Random Number is "1" Then Image will display
        if (num == 1)
        {
            ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
            ImageArray[1].Source = MoleImage;
        }

This is the StackPanel XAML:

    <Window x:Name="Window1" x:Class="WhackaMole.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="468.843" Width="666.045" OpacityMask="#FFF70D0D"                         Icon="mole2.png" Cursor="" >
<Grid OpacityMask="#FF5D1313">
    <Image Margin="422,191,-185,-69" Source="mole2.png" Stretch="Fill" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>

    <TextBlock HorizontalAlignment="Left" Margin="35,31,0,0" TextWrapping="Wrap"             VerticalAlignment="Top" Height="52" Width="595" FontSize="50" FontFamily="SimHei"><Run Language="en-ca" Text="Can You Catch the Mole?"/></TextBlock>
    <Button x:Name="NewGameBttn" Content="New Game" HorizontalAlignment="Left" Margin="77,0,0,16" VerticalAlignment="Bottom" Width="139" Height="50" FontSize="25" Click="NewGameBttn_Click"/>
    <Button x:Name="CloseBttn" Content="Close" HorizontalAlignment="Left" Margin="245,365,0,0" VerticalAlignment="Top" Width="76" Height="50" FontSize="29" Click="CloseBttn_Click"/>
    <StackPanel x:Name="StackPanel1" HorizontalAlignment="Left" Height="231" Margin="35,112,0,0" VerticalAlignment="Top" Width="525"/>

</Grid>
</Window>
1
  • 1
    What is the value of ImgNameMole? Also, is this image in your project? Commented May 7, 2013 at 18:16

2 Answers 2

4

As far as I can tell you're creating a new object of type Image but that Image really doesn't have anything to display. You need to set the Source of your Image. Here's an example stolen from MSDN.

Image myImage = new Image();
myImage.Source = new BitmapImage(new Uri("myPicture.jpg", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(myImage);

As townsean pointed out, you should probably create a Style for your Image where you can set common properties such as Height, Width etc.

Sign up to request clarification or add additional context in comments.

5 Comments

doh, I totally overlooked that part too :P I'll need to update my answer
Later on in the program I am giving the ImageArray a value, I will edit my question.
Then perhaps something is wrong with your Uri? Try specifying UriKind.RelativeOrAbsolute in the constructor for Uri If that doesn't fix it, please post your xaml.
I'll add the XAML to the question :) Thanks for your help!
Can you post a little bit more? That one line is not much to go by.
2

My guess is that since you are adding the items to a StackPanel, the StackPanel is choosing the default mins for height and width on the image (which is probably 0), and that is why you are not seeing anything.

Try setting a value for the Height and Width of the image and see if anything shows.

Also, as Tejas pointed out you are not setting the image source.

EDIT: Set the Image width like this:

Image myImage = new Image();
myImage.Width = 25;
myImage.Height = 25;

Do something like this in your for-loop where you first create the images.

5 Comments

I have a height and width set in the XAML :(
I don't see where you are setting the height and width for each image in xaml. Did you mean you set the height and width for the stackpanel?
Oh, yes. How would I set the image size? Sorry I am new to this :/
OH MAN THANK YOU! I'VE BEEN WORKING ON THIS FOR FOREVER! :)
No prob. I've been bitten many times with the StackPanel using default min values :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.