I used the DSOFile object to obtain the thumbnail inside the report then used AxHost to convert the returned object to an image I could display. This wasn't the solution I wanted but the DSOFile is freely distributable so I guess this will work until I find something better.
- Download and install the DSOFile DLL from Microsoft.
- Add a reference to **DSO OLE Document Properties Reader 2.1
- code
Here is my code, boiled down to the bare minimum:
namespace Ibs.Ui.OrderPrint
{
public partial class OrderPrintEdit
{
public OrderPrintEdit()
{
InitializeComponent();
}
#region -- reports_SelectedIndexChanged(sender, e) Event Handler --
private void reports_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DSOFile.OleDocumentPropertiesClass oleDocumentPropertiesClass = new DSOFile.OleDocumentPropertiesClass();
DirectoryInfo reportDirectory = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\Reports");
oleDocumentPropertiesClass.Open(reportDirectory + "\\" + reports.Text,true,DSOFile.dsoFileOpenOptions.dsoOptionDontAutoCreate);
Object thumbnail = oleDocumentPropertiesClass.SummaryProperties.Thumbnail;
if (thumbnail != null)
{
reportThumbnail.BackgroundImage = IPictureDispHost.GetPictureFromIPicture(thumbnail);
}
else
{
reportThumbnail.BackgroundImage = null;
}
oleDocumentPropertiesClass.Close(false);
}
catch (Exception ex)
{
}
}
#endregion
}
internal sealed class IPictureDispHost : AxHost
{
private IPictureDispHost() : base("{63109182-966B-4e3c-A8B2-8BC4A88D221C}")
{
}
/// <summary>
/// Convert the dispatch interface into an image object.
/// </summary>
/// <param name="picture">The picture interface</param>
/// <returns>An image instance.</returns>
public new static Image GetPictureFromIPicture(object picture)
{
return AxHost.GetPictureFromIPicture(picture);
}
}
}
I am filling a combobox with report names on the form load. In the SelectedIndexChanged event I get the Thumbnail object from the report and pass it to the conversion method. This should work for Office documents too.