WPF File Previewer Control

Project Description

The WPF File Previewer control allows developers to add a lightweight file viewer to their WPF applications with just three (3) lines of code.

The project includes source for the control as well as for a "demo" project. The screen-shots below are from the demo and show previews of a Visio drawing,  a PowerPoint slide, an mp3 file and a jpeg. (The icons to the right of the file names in the screen shots are from the ImageSource property of the control.)


Previewing a visio diagram Previewing a jpeg Previewing a PowerPoint slide Previewing an mp3 


Most of the heavy lifting for the control occurs in a class written by Brad Smith in his blog column, Preview Handlers Revisited.

My contributions, apart from packaging it as a WPF control, include cleaning up a couple of snags in the code-base that caused previewing to hang. (After previewing an Excel file, previewing a second Excel file would fail. Resizing the preview window would fail. Mht files failed with protected memory errors.) I also extended the range of previewable file types and added a nice little ImageSource function for displaying the icon corresponding to a file type. You can also databind both to it and the control's FileName property, as I have done in the demo source code.

Care and Feeding

  1. Download the WpfDocumentPreviewer.dll or the WpfDocumentPreviewer project source files. 
  2. Add a reference to WpfDocumentPreviewer in your project,
  3. Add a WpfDocumentPreviewer control to your WPF xaml (see the xaml below)
  4. Set the WpfDocumentPreviewer control's Filename property in your code behind (see the c# code below)
  5. Optionally, set an image control's Source property to the WpfDocumentPreviewer control's ImageSource.
<WpfDocumentPreviewer:PreviewerControl x:Name="previewControl" />
previewControl.FileName = od.FileName;

imgExtension.Source = previewControl.ImageSource;

What is a File Previewer?

A file previewer allows users to see the contents of a file without having to open the underlying application for the file type. Of course, the user's system still has to have the underlying application installed.

The previewer in this project is based on the Outlook 2007 attachment file previewer, which has origins as described below: 

"Every version of Microsoft Windows and Office brings new methods and approaches for improving your ability to see, interact with, and understand data... Outlook 2003 provided a reading pane for e-mail that made it easy to view the contents of a message without having to open it. You simply selected the message in the mail folder's list view, and the message was rendered in a side or bottom window. Outlook 2007 extends this concept by allowing you to view message attachments in that same preview pane, without having to double-click an attachment to open it in the appropriate viewer."
-- Stephen Toub, ‘View Data Your Way With Our Managed Preview Handler Framework‘ 

Last edited Jun 9, 2016 at 1:12 AM by onejbsmith, version 51