Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Show thumbnails of XPS documents

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
26 Jan 2010CPOL1 min read 42.8K   1.9K   40  
How to show thumbnails of XPS documents.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace XPS2Image
{
    public partial class XPSThumbnail : Form
    {
        XpsImage converter = null;
        public XPSThumbnail()
        {
            InitializeComponent();
            converter = new XpsImage();
            this.ClientSize = new Size(411, 300);
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog.ShowDialog(this);
            textBoxSelectedFile.Text = openFileDialog.FileName;
        }


        private void buttonShowThumbnail_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            converter.XpsFileName = openFileDialog.FileName;
            converter.OutputFormat = GetImageFormat();
            converter.OutputQuality = GetImageQuality();
            converter.GenerateThumbnail();
            pictureBox1.Image = Image.FromStream(converter.OutputStream);
            this.ClientSize = new Size(411, 708);
            this.Cursor = Cursors.Arrow;
        }

        private void saveImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            switch (converter.OutputFormat)
            {
                case OutputFormat.Jpeg:
                    saveThumbnail.Filter = "JPEG (*.jpg)|*.jpg";
                    break;
                case OutputFormat.Png:
                    saveThumbnail.Filter = "PNG (*.png)|*.png";
                    break;
                case OutputFormat.Gif:
                    saveThumbnail.Filter = "GIF (*.gif)|*.gif";
                    break;
                default:
                    saveThumbnail.Filter = "JPEG (*.jpg)|*.jpg";
                    break;
            }
            saveThumbnail.ShowDialog(this);
        }

        private OutputFormat GetImageFormat()
        {
            if (radioButtonJpeg.Checked)
                return OutputFormat.Jpeg;
            else if (radioButtonPng.Checked)
                return OutputFormat.Png;
            else if (radioButtonGif.Checked)
                return OutputFormat.Gif;
            else
                return OutputFormat.Jpeg;
        }

        private OutputQuality GetImageQuality()
        {
            if (radioButtonQualityLow.Checked)
                return OutputQuality.Low;
            else if (radioButtonQualityNormal.Checked)
                return OutputQuality.Normal;
            else if (radioButtonQualityGood.Checked)
                return OutputQuality.Good;
            else if (radioButtonQualitySuper.Checked)
                return OutputQuality.Super;
            else
                return OutputQuality.Normal;
        }

        private void saveThumbnail_FileOk(object sender, CancelEventArgs e)
        {
            FileStream stream = new FileStream(saveThumbnail.FileName, FileMode.Create, FileAccess.Write);
            converter.OutputStream.WriteTo(stream);
            stream.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)


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

Comments and Discussions