Click here to Skip to main content
12,755,827 members (38,168 online)
Click here to Skip to main content

Tagged as

Stats

24.7K views
1.6K downloads
39 bookmarked
Posted 25 Jan 2010

Show thumbnails of XPS documents

, 26 Jan 2010 CPOL
How to show thumbnails of XPS documents.
/*
 Copyright 2010 Pravesh Soni
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
using System.Windows.Xps.Packaging;
using System.Windows.Media;

namespace XPS2Image
{
    /// <summary>
    /// Imageformat
    /// </summary>
    public enum OutputFormat
    {
        Jpeg,
        Png,
        Gif
    }

    /// <summary>
    /// Image Quality
    /// </summary>
    public enum OutputQuality
    {
        Low,
        Normal,
        Good,
        Super
    }

    /// <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 = xpsDocument.GetFixedDocumentSequence();
            
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(this.XpsFileName);
            string fileExtension = string.Empty;

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

                case OutputQuality.Good:
                    imageQualityRatio *= 2.0;
                    break;

                case OutputQuality.Super:
                    imageQualityRatio *= 3.0;
                    break;
                default:
                    imageQualityRatio *= 1.0;
                    break;
            }

            DocumentPage documentPage = documentPageSequence.DocumentPaginator.GetPage(0);
            RenderTargetBitmap targetBitmap = new RenderTargetBitmap((int)(documentPage.Size.Width * imageQualityRatio), (int)(documentPage.Size.Height * imageQualityRatio), 96.0 * imageQualityRatio, 96.0 * imageQualityRatio, PixelFormats.Pbgra32);
            targetBitmap.Render(documentPage.Visual);
            
            

            bitmapEncoder.Frames.Add(BitmapFrame.Create(targetBitmap));
            string str4 = string.Format("{0}{1}", fileNameWithoutExtension, fileExtension);

            MemoryStream memoryStream = new MemoryStream();
            bitmapEncoder.Save(memoryStream);
            this.OutputStream = memoryStream;
            xpsDocument.Close();
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

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...

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