Search code examples
c#imageembedded-resourcemigradoc

Output image with build


I am using MigraDoc, and would like to insert an image in a document. The simplest way to do this, is to provide a path for the image file (rather than having it in the resources). So what I would like to do is, to include an image as a file, alongside the other files in the build. Then MigraDoc can use the image based on relative path.

But I have no clue how to do this or if it is even possible. So can I make my project include an image file, outside the resources.

P.S. I know that there is a work around, where you include the image in the resources, save it in a temporary folder before MigraDoc has to use it. It just seems like a simpler design, if the image is always available.

P.P.S. There is a work around, to make MigraDoc use images from the project resources (see accepted answer).


Solution

  • As shown on the MigraDoc site you can use images you have as a byte[] to create PDF files. This can easily be used with images from resources (as shown on the site).

    You just convert the image resource to a special string and pass that string where MigraDoc expects a filename.

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

    Link to official MigraDoc site:
    http://www.pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx

    And MigraDoc can also use images that are stored in the program folder with your assemblies. You can use the Assembly class to find out where your assembly is stored and search that folder for images that should be there. This has nothing to do with MigraDoc, just some basic .NET features.