下面的方法接受某个 pdf 文件的一些 FileInfo,然后继续将该 pdf 转换为位图。这种方法似乎随机工作。有时它会毫无问题地完成,有时它会在using (Bitmap bmp = image.ToBitmap())
零件上失败。一旦到达该行,我就会收到“参数无效”错误。我不知道如何修复这个随机错误,也没有进一步剖析它。任何帮助,将不胜感激
static void ParseOutEachBitmap(FileInfo[] pdfFiles)
{
string BmpPath = "C:\\temp\\bmps\\";
if (!Directory.Exists(BmpPath))
{
Directory.CreateDirectory(BmpPath);
}
using (MagickImageCollection images = new MagickImageCollection())
{
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new MagickGeometry(300, 300);
for (int p = 0; p < pdfFiles.Count(); p++)
{
images.Read(@"c:\temp\pdfs\" + pdfFiles[p].Name, settings);
int pageNumber = 1;
string pdfName = pdfFiles[p].Name;
foreach (MagickImage image in images)
{
using (Bitmap bmp = image.ToBitmap())
{
Console.WriteLine("PDF Filename: " + pdfName);
Console.WriteLine("Page Number: " + pageNumber + " of " + images.Count);
pageNumber++;
using (tessnet2.Tesseract tessocr = new tessnet2.Tesseract())
{
tessocr.GetThresholdedImage(bmp, System.Drawing.Rectangle.Empty).Save("c:\\temp\\bmps\\" + Guid.NewGuid().ToString() + ".bmp");
}
}
}
}
}
}