下面的方法接收某个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");
}
}
}
}
}
}