Why am I getting a “parameter invalid” exception in this code?

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 the line:

 Bitmap myBitmaps = new Bitmap(_fi[i].Name); 
+4
source share
3 answers

Passing the file name only to the Bitmap constructor, but you must pass the full path to the file using _fi[i].FullName

+11
source

@Lester is the correct answer (+1), but I wanted to say that you could shorten your implementation and make it more readable using some functional programming constructors:

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

Have you tried .FullName ? This should include the entire directory.

+1
source

Source: https://habr.com/ru/post/1395803/


All Articles