Andrew
Andrew

Reputation: 545

Creating Image Dynamically

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>

Upvotes: 1

Views: 5460

Answers (2)

Ashley Grenon
Ashley Grenon

Reputation: 9565

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.

Upvotes: 2

Tejas Sharma
Tejas Sharma

Reputation: 3440

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.

Upvotes: 4

Related Questions