我正在尝试将图像插入到我的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);
保存图片网址的代码
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 回答
.NET方法
Path.Combine
可以在这种情况下使用 .将绝对文件名传递给PDFsharp,PDFsharp将使用正确的文件 .
如果使用相对路径调用
XImage.FromFile
,也可以设置工作目录,但我会使用前一种方法 .这是获胜组合