Welcome GuestLogin

PDFsharp and MigraDoc Wiki

RSS RSS

Navigation




Quick Search
»

PoweredBy

Visit the new Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6 and find information about the new version for Windows, Linux, and other platforms.

MigraDoc: Images from Memory

RSS
Modified on 2015/09/10 15:45 by Thomas Hövel Categorized as Articles, MigraDoc Articles
Many users asked how to use images that are not stored in files on the local file system. Now we have a solution.

How to use images that are not stored in a file?

MigraDoc requires a filename to access images.

So what can you do if you want to use images from application resources, databases, or streams?

With PDFsharp 1.50 beta 2, a new feature was added: MigraDoc now accepts filenames that contain BASE64-encoded images with the prefix "base64:". In this case, the filename does not refer to a file, the filename contains all the bits of the bitmap in an ASCII string with the BASE64 encoding.

So the filename can be very long, depending on the size of the file.

A byte array (byte[] in C#) can easily be converted to a filename. If you have a stream, you can easily read it into a byte array and use it.

This routine shows how to do it:
static string MigraDocFilenameFromByteArray(byte[] image)
{
    return "base64:" +
           Convert.ToBase64String(image);
}

Here we have a routine that reads an image from a resource. This works for images that were added to your application using the Build Action "Embedded Resource".
static 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;
    }
}

And finally we see the two routines at work:
byte[] image = LoadImage("ImageFromResource.images.Color8A.bmp");
 
string imageFilename = MigraDocFilenameFromByteArray(image);
 
Document document = new Document();
Section section = document.AddSection();
section.AddImage(imageFilename);

Please note that the parameter needed for the LoadImage method will depend on the namespace of your application, the folder where you store the images, and the names of your images. The free dotPeek utility from JetBrains can be used to examine your compiled EXE file in order to find the strings that identify your resources.

Notes

  • The support for "base64:" was added with PDFsharp 1.50 beta 2 and cannot be used with older versions.
  • "base64:" can be used with MigraDoc only, not with PDFsharp functions. Since PDFsharp can use images from streams, this is not a problem. For PDFsharp you can create a MemoryStream from the byte array and use this stream to create an XImage.


Visit the new Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6 and find information about the new version for Windows, Linux, and other platforms.



Miscellaneous
Home
PDFsharp
FAQ
Samples
Articles
MigraDoc
FAQ
Samples
Articles

ScrewTurn Wiki version 3.0.5.600. Some of the icons created by FamFamFam.

Impressum - Privacy Policy, Data Protection Declaration, Legal Notice