0

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];
1
  • what do you want to do - show a list of images ? Commented Apr 4, 2013 at 19:09

2 Answers 2

2
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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Image is not a base class of BitmapImage. I guess you are confusing it with ImageSource.
And did you know that BitmapImage has a constructor with a Uri parameter? That would save you all the BeginInit and EndInit stuff.
Ah really? I was just going off the original poster's code. I'll try to clarify.
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++
    }

Comments

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.