Search code examples
imagemigradoc

MigraDoc - How to Add image from External URL


I am not able to add image that is saved at a URL something like "http://host/img.png" using below code snippet:

section.AddImage(url);

However if I use url as a relative url of image that is present on my disk then this is working fine.

Is this limitation of MigraDoc?
Can someone provide workaround, code samples to over come this?


Solution

  • MigraDoc works with local files (relative or absolute paths), not with files that are somewhere on the Internet.

    AddImage() needs a path, not a URL.

    You can download the image from the URL and either store it in a local file or store it in a string using BASE64 encoding and pass that string to MigraDoc.
    You will have control over accessing the image from the web and can properly deal with problems.

    See this article on BASE64 encoding for MigraDoc images:
    http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx