使用 XGraphics 时如何解决 "can not convert from string[] to string" 错误

How to solve "can not convert from string[] to string" error when using XGraphics

我有一个按钮,用于从我的静态目录 (C:\myfolder) 中的所有图像创建 pdf。 但我有错误

"can not convert from string[] to string" for the string "files" in the line below:

DrawImage(gfx, files, 0, 0, (int)page.Width, (int)page.Height);

我该如何解决这个错误? 谢谢。 这是我的代码:

string[] files = Directory.GetFiles(@"C:\myfolder");
PdfDocument document = new PdfDocument();
document.Info.Title = "Created in Pdf";

foreach (string img in files)
{
    PdfPage page = document.AddPage();
    XGraphics gfx = XGraphics.FromPdfPage(page);
    DrawImage(gfx, files, 0, 0, (int)page.Width, (int)page.Height);
}
if(document.PageCount > 0)
{
    document.Save(@"C:\test.pdf");
}

void DrawImage(XGraphics gfx, string jpegSamplePath, int x, int y, int width, int height)
{
    XImage image = XImage.FromFile(jpegSamplePath);
    gfx.DrawImage(image, x, y, width, height);
}

对于 files 列表中的每个 img,您想绘制 img
因此 DrawImage(gfx, files,...) 应该是 DrawImage(gfx, img,...)

查看 DrawImage:

的方法声明
void DrawImage(XGraphics gfx, string jpegSamplePath, ...

第二个参数是包含文件名的 string,而不是 string 的数组。

只需将代码中的错误更改为使用文件名 (img) 而不是文件名s (files) 即可:

DrawImage(gfx, img, 0, 0, (int)page.Width, (int)page.Height);