Extraction of embedded inline images in InfoPath 2010
At work, we needed to extract images from rich text boxes in a InfoPath 2010 form. These image files are stored to a temporary disk area and used to create a HTML preview of a document based on the XML content in the form, and also for uploading those images to a SharePoint image library, separated from their source document. In InfoPath 2010, all images are stored inline on the img-elements, in base64 encoding in the attribute xd:inline.
Our forms requires the InfoPath filler application, not web forms hosted by SharePoint. I don’t think that will work. The code in the example FormCode.cs below needs full trust, since it writes to the hard disk. It’s not very useful as it is written here, but it is just to illustrate the extraction mechanism.
If you upload the images to a SharePoint library like we do, you also have to add a src-attribute in the img-element, so that it points to its new address.
I don’t include the form in this post, but it’s simple: a rich text box with embedded images enabled, connected to one field, and a button with the id BTN_EXTRACT_IMAGES.
If images are of a more photographic nature than our images, then perhaps JPG is a better storage format than PNG.
using Microsoft.Office.InfoPath;
using System;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Drawing;
using mshtml;
namespace ExtracImagesForm
{
public partial class FormCode
{
public void InternalStartup()
{
((ButtonEvent)EventManager.ControlEvents["BTN_EXTRACT_IMAGES"]).Clicked += new ClickedEventHandler(BTN_EXTRACT_IMAGES_Clicked);
}
public void BTN_EXTRACT_IMAGES_Clicked(object sender, ClickedEventArgs e)
{
ExtractEmbeddedImages(this.MainDataSource.CreateNavigator());
}
public void ExtractEmbeddedImages(XPathNavigator navigator)
{
XPathNavigator document = navigator.Clone();
string nsXd = NamespaceManager.LookupNamespace("xd");
string nsXhtml = NamespaceManager.LookupNamespace("xhtml");
if (nsXhtml == null)
NamespaceManager.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
XPathNodeIterator imageNodes = document.Select("//xhtml:img[@xd:inline]", NamespaceManager);
foreach (XPathNavigator imageNode in imageNodes)
{
XPathNavigator inlineAttribute = imageNode.Clone();
inlineAttribute.MoveToAttribute("inline", nsXd);
string imageString = inlineAttribute.Value;
byte[] imageDecoded = Convert.FromBase64String(imageString);
string directoryPath = @"C:\temp";
if (!Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);
string imageName = Guid.NewGuid().ToString();
string imageFilePath = Path.Combine(directoryPath, imageName) + ".png";
using (MemoryStream imageMemStream = new MemoryStream(imageDecoded))
using (FileStream fs = new FileStream(imageFilePath, FileMode.Create))
using (Image image = Image.FromStream(imageMemStream))
{
image.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
} //class
} // ns