0

如何将一些源代码放入Array名为 WPF 控件的源代码中Image?我找到了这个论坛,但是如何制作数组?

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];
4

2 回答 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...

或者,使您的图像工厂成为一个函数并使用 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();
}
于 2013-04-04T16:35:51.577 回答
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++
    }
于 2014-01-30T04:24:21.223 回答