为什么我在此代码中获得“参数无效”异常?

时间:2012-02-10 16:20:30

标签: c# .net

private void button8_Click(object sender, EventArgs e)
{
    List<long> averages;
    long res = 0;
    _fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
    averages = new List<long>(_fi.Length);
    for (int i = 0; i < _fi.Length; i++)
    {
        Bitmap myBitmaps = new Bitmap(_fi[i].Name);
        //long[] tt = list_of_histograms[i];
        long[] HistogramValues = GetHistogram(myBitmaps);
        res = GetTopLumAmount(HistogramValues,1000);
        averages.Add(res);
    }
}

例外是在线:

Bitmap myBitmaps = new Bitmap(_fi[i].Name);

3 个答案:

答案 0 :(得分:11)

您只是将文件名传递给Bitmap构造函数,但实际上应该使用_fi[i].FullName

传递文件的完整路径

答案 1 :(得分:3)

@Lester是正确的答案(+1),但我确实想说你可以缩短你的实现,并通过使用一些函数式编程结构使它更具可读性:

var averages = new DirectoryInfo(subDirectoryName)
    .GetFiles("*.bmp")
    .Select(t => new Bitmap(t.FullName))
    .Select(GetHistogram)
    .Select(v => GetTopLumAmount(v, 1000))
    .ToList();

答案 2 :(得分:1)

你试过.FullName吗?这应该包括整个目录。