WPF中的图像数组

时间:2013-04-04 16:31:26

标签: c# wpf arrays image

如何将Array某些来源放入名为Image的WPF控件中?我发现这个论坛,但如何制作阵列?

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

tmpimage.Source = logo;

但是我需要像这样的字符串:

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

2 个答案:

答案 0 :(得分: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...

或者,使您的图像工厂成为一个函数并使用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();
}

答案 1 :(得分:1)

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++
    }