Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "parameter is not valid" exception on this code?

Tags:

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);
    }
}

The exception is on the line:

Bitmap myBitmaps = new Bitmap(_fi[i].Name);
like image 399
user1196715 Avatar asked Dec 07 '22 16:12

user1196715


2 Answers

You're only passing the file name to the Bitmap constructor, but you should actually pass the full path to the file using _fi[i].FullName

like image 169
Adi Lester Avatar answered Dec 09 '22 05:12

Adi Lester


@Lester is the right answer (+1), but I did want to say you could shorten your implementation and make it a little more readable by using some functional programming constructs:

var averages = new DirectoryInfo(subDirectoryName)
    .GetFiles("*.bmp")
    .Select(t => new Bitmap(t.FullName))
    .Select(GetHistogram)
    .Select(v => GetTopLumAmount(v, 1000))
    .ToList();
like image 21
J. Holmes Avatar answered Dec 09 '22 04:12

J. Holmes