Foreach位图在文件夹中

本文关键字:文件夹 位图 Foreach | 更新日期: 2023-09-27 18:25:19

下面是一些使用TessNet的OCR工具扫描文件夹中的每个位图并将OCR信息处理为List<tessnet2.Word> result的代码。但我的前臂似乎无法正常工作。我得到以下错误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);                                                                             
                                    }                                    
                            }

Foreach位图在文件夹中

看起来你想要

foreach(string s in Directory.EnumerateFiles(dir, "*.bmp"))
{
    using(Bitmap bmp = new Bitmap(s))
    {
      //code here
     }
 }

您的错误是因为foreach循环必须有要循环的内容,DirectoryInfo只会向您提供有关目录的信息