如何设置pdfsharp图像的路径

时间:2015-03-24 09:44:34

标签: pdfsharp

我正在尝试将图像插入到我的Pdf中。我正在使用url从我的数据库中检索图像。它没有使用文档的正确路径。例外表明它正在寻找

的图像
  

C:\ MeasurementPictures \凯利 - 字前3-24-2015-4-37-AM.jpg

图像的实际物理路径是

  

C:\发展\ FitFactor \ FitFactor \ MeasurementPictures \凯利 - 字前3-24-2015-4-37-AM.jpg

image

XImage image = XImage.FromFile(model.FrontPicture.PictureUrl);
            gfx.DrawImage(image, 20, 100, 250, 140);

保存图片网址的代码

string root = HttpContext.Current.Server.MapPath("~/MeasurementPictures/");
  string vPath = root.Replace(@"C:\Development\FitFactor\FitFactor", "~").Replace("\\", "/").Replace("~/", "/");

  string fileName = String.Format("{0}-{1}-{2}{3}", clientName, model.PictureType, dt, clientExtension);
  string combination = Path.Combine(vPath, fileName);

   model.PictureUrl = combination;

  string physicalPath = HttpContext.Current.Server.MapPath("/MeasurementPictures");
  string relativePath = Path.Combine(physicalPath, fileName).Replace("\\", "/");

我会使用这样的东西吗?

string root = HttpContext.Current.Server.MapPath("~/MeasurementPictures/");
            string vPath = root.Replace(@"C:\Development\FitFactor\FitFactor", "~").Replace("\\", "/").Replace("~/", "/");
            string relativePath = Path.Combine(vPath, model.FrontPicture.PictureUrl).Replace("\\", "/");

            XImage image = XImage.FromFile(relativePath);
            gfx.DrawImage(image, 20, 100, 250, 140);

2 个答案:

答案 0 :(得分:0)

.NET方法Path.Combine可以在这种情况下使用。

将绝对文件名传递给PDFsharp,PDFsharp将使用正确的文件。

如果您使用相对路径调用XImage.FromFile,也可以设置工作目录,但我会使用前一种方法。

答案 1 :(得分:0)

 string filePath = model.FrontPicture.PictureUrl.Replace("C:", " ");
 string completeFilePath = HttpContext.Current.Server.MapPath(filePath);
 XImage image = XImage.FromFile(Path.Combine(completeFilePath));
 gfx.DrawImage(image, 20, 100, 250, 140);

这是获胜组合

相关问题