【问题标题】:How to solve "can not convert from string[] to string" error when using XGraphics使用 XGraphics 时如何解决“无法从字符串 [] 转换为字符串”错误
【发布时间】:2019-10-10 10:49:36
【问题描述】:

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

对于下面一行中的字符串“files”,“can not convert from string[] to string”:

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);
}

【问题讨论】:

    标签: c# arrays string c#-4.0 pdfsharp


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      查看DrawImage 的方法声明:

      void DrawImage(XGraphics gfx, string jpegSamplePath, ...
      

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

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

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

      【讨论】:

        猜你喜欢
        • 2017-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多