user1638121
user1638121

Reputation: 167

Array of image in WPF

How to put into Array some source to WPF Controls called Image? O this forum i find, but how to make array?

BitmapImage logo = new BitmapImage()
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/img/3.jpg");
logo.EndInit();

tmpimage.Source = logo;

But i need sometring like this:

Image[] img = new Image[3];
img[0].Source = new Uri("pack://application:,,,/img/3.jpg");
tmpimage.Source = img[0];

Upvotes: 0

Views: 5046

Answers (2)

Franck Magazine
Franck Magazine

Reputation: 1

Dynamic Array of BitmapImage :

    BitmapImage[] iHab;

    BitmapImage CreateBitmap(string uri)
        { 
        return new BitmapImage(new Uri(uri));
        }

    BitmapImage[] GetImages()
        {
        string currDir = Directory.GetCurrentDirectory();
        string[] imageUris;

        //Get directory path of myData 
        string temp = currDir + "\\Media\\hcia\\";
        imageUris = Directory.GetFiles(temp, "habitation*.png");
        return imageUris.Select(CreateBitmap).ToArray();  
        }

    private void Rec_hab_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        iHab = GetImages();
        pointer.Source = iHab[7]; // the 7th image : can be manipulated with an i++
    }

Upvotes: 1

Alex
Alex

Reputation: 7919

Image[] images = new Image[3] { new Image(), new Image(), new Image() };
images[0].Source = new BitmapImage(new Uri("pack://application:,,,/img/3.jpg"));
images[1].Source = new BitmapImage... // etc...

Alternatively, make your image factory a function and use LINQ:

Image CreateBitmap(string uri)
{ 
    return new Image() { Source = new BitmapImage(new Uri(uri)) };
}

Image[] GetImages()
{
    var imageUris = new[]
    {
        "pack://application:,,,/img/3.jpg", 
        "pack://application:,,,/img/elephant.jpg", 
        "pack://application:,,,/img/banana.jpg"
    };
    return imageUris.Select(CreateBitmap).ToArray();
}

Upvotes: 2

Related Questions