Search code examples
pdfsharp

How to set the path for a pdfsharp image


I am trying to insert images into my Pdf. I am retrieving the image from my db with the url. It is not using the right path to the document. the exception shows that it is looking for the image at

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

the actual physical path to the images is

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

image

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

Code to Save Picture 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("\\", "/");

Would I use something like this?

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

Solution

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

    Here is the winning combination