问题
So below is some code that uses TessNet's OCR tool to scans each bitmap in the folder and processes the OCR information into List<tessnet2.Word> result. But I can't seem to get the foreach to work right.
I get the following error foreach statement cannot operate on variables of type 'System.IO.DirectoryInfo' because 'System.IO.DirectoryInfo' does not contain a public definition for 'GetEnumerator'
DirectoryInfo diBMP = new DirectoryInfo("c:\\temp\\bmps");
foreach (Bitmap bmp in diBMP)
{
using (tessnet2.Tesseract tessocr = new tessnet2.Tesseract())
{
tessocr.Init(@"C:\Users\Matt Taylor\Documents\Visual Studio 2012\Projects\TessNet2\TessNet2\bin\Debug\tessdata", "eng", false);
tessocr.GetThresholdedImage(bmp, System.Drawing.Rectangle.Empty).Save("c:\\temp\\" + Guid.NewGuid().ToString() + ".bmp");
Console.WriteLine("Normal version");
List<tessnet2.Word> result = ocr.DoOCRNormal(bmp, "eng");
CheckANDCorrectPDForientation(result, pdfFiles);
}
}
回答1:
It looks like you want
foreach(string s in Directory.EnumerateFiles(dir, "*.bmp"))
{
using(Bitmap bmp = new Bitmap(s))
{
//code here
}
}
Your error is because a foreach loop must have something to loop through, DirectoryInfo will just give you information about the directory
来源:https://stackoverflow.com/questions/17930859/foreach-bitmap-in-folder