/// <summary>
/// Gets "Assembly Resource" image encoded as base64 string (which MigraDoc knows how to
/// handle), ready to be consumed by MigraDoc components. Images are located in ~/Resources/images/*
/// and they need to be marked as "Embedded Resource" and set to "Copy if newer".
/// </summary>
/// <see cref="http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx"/>
/// <param name="imageName">Image Assembly Resource name.</param>
/// <returns></returns>
protected string GetMigraDocImage(string imageName)
{
// Add your namespace + project path to the image you want to embed
var assemblyResourceName = String.Format("Your.Layer.Namespace.Resources.images.{0}", imageName);
var image = LoadAssemblyImage(assemblyResourceName);
var imageFilename = ConvertToMigraDocFilename(image);
return imageFilename;
}
/// <summary>
/// Loads assembly image from the project and converts it to a byte array.
/// </summary>
/// <see cref="http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx"/>
/// <param name="name">Assembly File Name.</param>
/// <returns>Image data array.</returns>
private byte[] LoadAssemblyImage(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;
}
}
/// <summary>
/// Converts image data array to base64 string. Used for integrating images into MigraDoc PDF's.
/// </summary>
/// <see cref="http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx"/>
/// <param name="image">Image data array.</param>
/// <returns>Image as base64 string.</returns>
protected string ConvertToMigraDocFilename(byte[] image)
{
return String.Format("base64:{0}", Convert.ToBase64String(image));
}