[英]How to create a collection of images from a directory
I'm looking for how to create a collection of images from a directory and then instantiate all the images dynamically.我正在寻找如何从目录创建图像集合,然后动态实例化所有图像。
Actually I instantiate my images as follows:实际上,我按如下方式实例化我的图像:
private BitmapSource orange = BitmapUtil.FromImages("orange.png");
private BitmapSource lemon = BitmapUtil.FromImages("lemon.png");
private BitmapSource apple = BitmapUtil.FromImages("apple.png");
But the problem is that, let's say someone add new fruits in the directory that contains all these images of fruits.但问题是,假设有人在包含所有这些水果图像的目录中添加了新水果。 I want to instantiate all of these fruits dynamically so when we run the program, it checks for all the images in the folder
/fruits/...
and then create this list of elements.我想动态地实例化所有这些水果,所以当我们运行程序时,它会检查文件夹
/fruits/...
中的所有图像,然后创建这个元素列表。
Edit: I want to do this using BitmapSource and BitmalUtil.FromImages because I still want to manipulate those images within a method.编辑:我想使用 BitmapSource 和 BitmalUtil.FromImages 来做到这一点,因为我仍然想在一个方法中操作这些图像。 I don't want to store them randomly in a list.
我不想将它们随机存储在列表中。
Something like this:像这样的东西:
List<BitmapSource> images = new List();
foreach (var filePath in Directory.GetFiles("Fruit"))
{
images.Add(BitmapUtil.FromImages(filePath));
}
You may as well use LINQ:你也可以使用 LINQ:
var images = Directory.EnumerateFiles("Fruit")
.Select(f => BitmapUtil.FromImage(f))
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.