Click here to Skip to main content
13,862,120 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


39 bookmarked
Posted 26 Jan 2010
Licenced CPOL

Show thumbnails of XPS documents

, 26 Jan 2010
Rate this:
Please Sign up or sign in to vote.
How to show thumbnails of XPS documents.



This application was designed as a proof of concept for generating thumbnails/images from an XPS document.


Recently, I came across a requirement for displaying thumbnails of XPS documents in a SharePoint document library. As a hardcore developer, I'm not in favor of readymade third-party components unless and until you really need them. Hence, I decided to create an image generator myself. I explored the native .NET methods for reading XPS documents. After exploring, I found that in .NET Framework 3.0, there is a managed DLL called ReachFramework.dll, which has all the necessary classes and methods for reading and writing XPS documents.


Class Diagram


XpsThumbnail Class

This is the one and only class which actually generates the thumbnails from XPS documents using the GenerateThumbnail method:



OutputFormatOutput format for the generated image.
OutputQualityQuality for the generated image.
OutputStreamMemoryStream object to be returned.
XpsFileNameXPS document of which an image needs to be generated.

Complete Class Listing

/// <summary>
/// Provides methods for converting XPS document in to various image format
/// </summary>
public class XpsImage
    private BitmapEncoder bitmapEncoder = null;
    /// <summary>
    /// Default constructor
    /// </summary>
    public XpsImage()

    /// <summary>
    /// Sets the XPS file to be read
    /// </summary>
    public String XpsFileName { private get; set; }

    /// <summary>
    /// Gets or Sets the image format for thumbnail
    /// </summary>
    public OutputFormat OutputFormat {get; set; }

    /// <summary>
    /// Gets or Sets the image quality for thumbnail
    /// </summary>
    public OutputQuality OutputQuality { private get; set; }
    /// <summary>
    /// Returns the Memory stream of generated thumbnail
    /// </summary>
    public MemoryStream OutputStream { get; private set; }

    /// <summary>
    /// Generate the thumbnail of given document
    /// and populates the ThumbnailStream property
    /// </summary>
    public void GenerateThumbnail()
        XpsDocument xpsDocument = 
          new XpsDocument(this.XpsFileName, FileAccess.Read);
        FixedDocumentSequence documentPageSequence = 
        string fileNameWithoutExtension = 
        string fileExtension = string.Empty;

        switch (this.OutputFormat)
            case OutputFormat.Jpeg:
                fileExtension = ".jpg";
                bitmapEncoder = new JpegBitmapEncoder();
            case OutputFormat.Png:
                fileExtension = ".png";
                bitmapEncoder = new PngBitmapEncoder();
            case OutputFormat.Gif:
                fileExtension = ".gif";
                bitmapEncoder = new GifBitmapEncoder();
                fileExtension = ".jpg";
                bitmapEncoder = new JpegBitmapEncoder();
        double imageQualityRatio = 1.0;
        switch (this.OutputQuality)
            case OutputQuality.Low:
                imageQualityRatio /= 2.0;

            case OutputQuality.Good:
                imageQualityRatio *= 2.0;

            case OutputQuality.Super:
                imageQualityRatio *= 3.0;
                imageQualityRatio *= 1.0;

        DocumentPage documentPage = 
        RenderTargetBitmap targetBitmap = new RenderTargetBitmap(
          (int)(documentPage.Size.Width * imageQualityRatio), 
          (int)(documentPage.Size.Height * imageQualityRatio), 
          96.0 * imageQualityRatio, 96.0 * imageQualityRatio, 

        string str4 = string.Format("{0}{1}", 
                       fileNameWithoutExtension, fileExtension);
        MemoryStream memoryStream = new MemoryStream();
        this.OutputStream = memoryStream;

Using the Code

The class itself is a very simple class and is made for simplicity. Below is a small snippet of how to utilize the class:

converter.XpsFileName = "XPSdocument.xps";
converter.OutputFormat = OutputFormat.Png;
converter.OutputQuality = OutputQuality.Super;


The sample application provided with this article demonstrates the utilization of the code. In the demo application, you need to save the image by invoking a context menu from the thumbnail to see the output as I'm using an in-memory image stream to display the image on the picture box.

And, please don't hesitate to provide feedback, good or bad! I hope you enjoyed this article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pravesh Soni
United States United States
My name is Pravesh Soni and I’m software professional. I got involved with computers since last two decades starting with playing games developed in BASIC. I am a tech enthusiast and I love writing code, blogging, science fiction, cartoons, music, robotics and I am a big Microsoft fan too.

I started programming in classic asp but later switch to .net and now at present I have almost 15 years of development experience in Microsoft technologies. I got involved in customized software development in my career of programming, now contributing to some open source community development.

You may also be interested in...

Comments and Discussions

BugGetting exception Pin
NIRAVRANA2-Sep-13 0:13
memberNIRAVRANA2-Sep-13 0:13 
GeneralRe: Getting exception Pin
NIRAVRANA2-Sep-13 0:15
memberNIRAVRANA2-Sep-13 0:15 
GeneralMy vote of 5 Pin
masbaaz3-Jun-13 3:07
membermasbaaz3-Jun-13 3:07 
GeneralXps document thumbnail Pin
rohinisreekanth25-Nov-10 19:25
memberrohinisreekanth25-Nov-10 19:25 
GeneralRe: Xps document thumbnail Pin
Pravesh Soni27-Dec-10 4:02
memberPravesh Soni27-Dec-10 4:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190214.1 | Last Updated 26 Jan 2010
Article Copyright 2010 by Pravesh Soni
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid