-1

I have problem with set correct file path to image in my project which is deployed with ClickOnce. The 3rd party library for generating pdf documents needs path of image as input for method.

public Image AddImage(string fileName);

I'm using following library for generating pdf documents - https://www.nuget.org/packages/PDFsharp-MigraDoc-gdi/1.50.5147/

The image is located in class library project which is linked to WPF project.

Image has following settings in visual studio.

enter image description here

But after install, image does not appear in instalation location.

If it would appear I would use following code for get path of image.

string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); \\+image name

What am I doing wrong?

Should I use another approach?

magino
  • 109
  • 9
  • Is the image file included under Project->Properties->Publish->Application Files? – mm8 Jun 25 '20 at 15:50
  • You can set Build Action to Resource or Embedded Resource to have the image included in the DLL. You app can then acquire a Stream to use the image at runtime. – I liked the old Stack Overflow Jun 26 '20 at 08:55
  • @mm8 no it is not. How can I add it there? .. I have the same settings for log.config file in WPF project and its there. I do not understand why the image.png does not behave the same with those settings. – magino Jun 28 '20 at 21:27
  • @IlikedtheoldStackOverflow ok but how can I pass the path of an image to method, if the image is included in dll. I think I tried this as first with following path, but it does not worked. `row.Cells[2].AddParagraph().AddImage("pack://application:,,,/Resources/image.png");` Is this correct file path for included resource? – magino Jun 28 '20 at 21:32
  • 1
    @magino For resources you have to obtain a `Stream`, read the stream, and convert the image data to a BASE64-encoded pseudo-filename. See also: http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx You cannot use `AddImage` with URLs, neither `pack:` protocol nor any other protocol. I didn't pay attention to the MigraDoc tag - I had my mind set to PDFsharp where `Streams` can be used directly. – I liked the old Stack Overflow Jun 29 '20 at 08:45
  • @magino: You could add the image to the WPF application project that you are publishing as a [link](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/9f4t9t92(v=vs.100)?redirectedfrom=MSDN). – mm8 Jun 29 '20 at 13:58
  • @IlikedtheoldStackOverflow thanks that worked! But I still think about how I could include that image so that I can find it in instalation folder after click once publish (just from curiosity). – magino Jun 29 '20 at 20:41
  • @mm8 have tried add image as link, but after publish and installation nothing appears in installation folder nor in Application Files in Project->Properties->Publish->Application Files. – magino Jun 29 '20 at 20:44
  • @magino: Then you probably added the link in the wrong project. – mm8 Jun 30 '20 at 12:19
  • @mm8 ou, the link should be to the main project? ye so thats why it did not showed up. Thanks for help! – magino Jun 30 '20 at 14:35

1 Answers1

0

Thanks to @IlikedtheoldStackOverflow I manage to include image to solution as embedded resource, which is also working after ClickOnce publish.

  1. I reproduced steps from from pdfsharp doc -pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx
  2. Changed Build Action to Embedded resource

enter image description here

  1. Created correct path to image in format - Namespace.FolderWhereIsImage.NameOfImage.png

SAMPLE CODE from pdfsharp doc

private string MigraDocFilenameFromByteArray(byte[] image)
{
   return "base64:" + Convert.ToBase64String(image);
}

private byte[] LoadImage(string name)
{
   var assembly = Assembly.GetExecutingAssembly();

   using (Stream stream = assembly.GetManifestResourceStream(name))
   {
      if (stream == null)
          throw new ArgumentException("No resource with name " + name);

      int count = (int)stream.Length;
      byte[] data = new byte[count];
      stream.Read(data, 0, count);
      return data;
   }
}


byte[] image = LoadImage("SampleProject.Resources.logo.png");
string imageFilename = MigraDocFilenameFromByteArray(image);
row.Cells[2].AddParagraph().AddImage(imageFilename);

"SampleProject.Resources.logo.png" .. Resources is name of folder in the project and SampleProject is project name.

magino
  • 109
  • 9