Foreach bitmap in folder

我们两清 提交于 2019-12-25 10:55:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!