gpt4 book ai didi

c# foreach 文件夹中的图像

转载 作者:行者123 更新时间:2023-12-03 21:50:21 27 4
gpt4 key购买 nike

下面是一些代码

  1. 创建目录
  2. 使用 MagickNet 将 PDF 转换为单独的 BMP 图像并将其存储在文件夹 (ImagePath) 中
  3. 然后它使用 TessNet2 扫描该文件夹中的每个图像以解析出信息

我似乎无法获取扫描 ImagePath 文件夹的 foreach 循环。有什么帮助吗?

现在的错误是在 'foreach' 语句上,它说“无法将类型 'char' 转换为 '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");
}
}
}

最佳答案

您可以使用 Directory.GetFiles 返回目录中的所有文件名并从那里创建您的位图

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

但如果该文件夹中还有其他文件,您应该添加一个过滤器

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

关于c# foreach 文件夹中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17582015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com