Below is some code that

  1. creates a directory
  2. uses MagickNet to covert a PDF into separate BMP images and stores it in a folder (ImagePath)
  3. Then it uses TessNet2 to scan each image in that folder to parse out information

I can't seem to get the foreach loop that scans the ImagePath folder. Any help?

The error right now is on the 'foreach' statement and it says "Cannot convert type 'char' to 'System.Drawing.Image'"

static void Main(string[] args)
    {
        string ImagePath = exePath + "\\Images"; 
        if (!Directory.Exists(ImagePath))
        {
            Directory.CreateDirectory(ImagePath);
        }

        MagickReadSettings settings = new MagickReadSettings();
        settings.Density = new MagickGeometry(300, 300);

        using (MagickImageCollection images = new MagickImageCollection())
        {
            images.Read(@"D:\Test\ABStest.pdf",settings);
            int page = 1;
            foreach (MagickImage image in images)
            {
                image.Write(ImagePath + "\\ABS" + page + ".bmp");
                page++;
            }
        }



        foreach (Image item in ImagePath)
        {
            using (Bitmap bmp = new Bitmap(item))
            {
                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, Rectangle.Empty).Save("c:\\temp\\" + Guid.NewGuid().ToString() + ".bmp");
                // Tessdata directory must be in the directory than this exe
                Console.WriteLine("Multithread version");
                ocr.DoOCRMultiThred(bmp, "eng");
                //Console.WriteLine("Normal version");
                //ocr.DoOCRNormal(bmp, "eng");
            }
        }
    }       
有帮助吗?

解决方案 2

You don't need to save the file to disk. You could use the .ToBitmap() method of MagickImage.

foreach (MagickImage image in images)
{
  using (Bitmap bmp = image.ToBitmap())
  {
    tessnet2.Tesseract tessocr = new tessnet2.Tesseract();
    // etc...
  }
}

其他提示

You can use Directory.GetFiles to return all the filenames in the directory and create your Bitmaps from there

foreach (string imageFileName in Directory.GetFiles(ImagePath))
{
    using (Bitmap bmp = new Bitmap(imageFileName))
    {
    }
}

But if there are other files in that folder you should add a filter

foreach (string imageFileName in Directory.GetFiles(ImagePath, "*.jpg"))
{
    using (Bitmap bmp = new Bitmap(imageFileName))
    {
    }
}

You are looping through String ImagePath, which gives you a collection of characters. You need to search through the directory with Directory.GetFiles(), and load the images with Image.FromFile():

foreach (String itemPath in Directory.GetFiles(ImagePath))
{
    using (Image item = Image.FromFile(itemPath))
    {
        ...
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top