如何设置pdfsharp图像的路径

How to set the path for a pdfsharp image

我正在尝试将图像插入到我的 Pdf 中。我正在使用 url 从我的数据库中检索图像。它没有使用正确的文档路径。异常显示它正在寻找位于

的图像

C:\MeasurementPictures\Kelly-Word-Front-3-24-2015-4-37-AM.jpg

图像的实际物理路径是

C:\Development\FitFactor\FitFactor\MeasurementPictures\Kelly-Word-Front-3-24-2015-4-37-AM.jpg

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

保存图片的代码URL

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

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

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

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

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

这是获胜组合