C#打开多个图像到阵列
我是C#新手,我正在尝试将多个图像打开到一个数组中,以便以后操作它们的像素,这是我目前的代码:C#打开多个图像到阵列,c#,arrays,C#,Arrays,我是C#新手,我正在尝试将多个图像打开到一个数组中,以便以后操作它们的像素,这是我目前的代码: private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp"; if (openFileDialog1.ShowDialog() == Dialo
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Bitmap[] images = new Bitmap(openFileDialog1.FileNames);
MessageBox.Show(images.Length+" images loaded","",MessageBoxButtons.OK);
}
}
这条线路有问题
Bitmap[] images = new Bitmap(openFileDialog1.FileNames);
你能帮我吗?使用:
images = openFileDialog1.FileNames.Select(fn=>new Bitmap(fn)).ToArray();
因为openFileDialog1.FileNames是字符串数组,位图构造函数需要单个图像文件名
Bitmap[] images = new Bitmap(openFileDialog1.FileNames);
Bitmap[] images // Is an array of Bitmap
new Bitmap(openFileDialog1.FileNames); // Returns a single (new) Bitmap
我建议使用列表。当你刚接触C#时,正如Pavel Kymets所建议的那样,使用foreach比使用LinQ容易得多
List<Bitmap> images = new List<Bitmap>();
foreach(string file in openFileDialog1.FileNames) {
images.Add(new Bitmap(file));
}
列表图像=新列表();
foreach(openFileDialog1.FileNames中的字符串文件){
添加(新位图(文件));
}
或者,如果您还没有准备好使用lambdas,可以使用
openFileDialog1.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
List<BitMap> images = new List<BitMaps>()
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach(string fileName in openFileDialog1.FileNames)
{
images.Add(new Bitmap(fileName));
}
}
MessageBox.Show(String.Format("{0} images loaded",images.Count),"",MessageBoxButtons.OK);
openFileDialog1.Filter=“图像文件(*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;*.bmp”;
列表图像=新列表()
如果(openFileDialog1.ShowDialog()==DialogResult.OK)
{
foreach(openFileDialog1.FileNames中的字符串文件名)
{
添加(新位图(文件名));
}
}
Show(String.Format(“{0}已加载图像”,images.Count),”,MessageBoxButtons.OK);