« Windows XP tip for quicker opening a program from Start menu | Main | Customized search box and live search macro »

Create thumbnails of PowerPoint presentations in C#

It's sometimes useful to generate JPEG thumbnails of office documents programmatically.  With Office object library, it's very convenient to do this in C#.NET.  If it's for a quick and dirty task, an easy way is to convert the office document into a webpage and then use a WebBrowser control to generate the thumbnail.  For a more performance oriented task, customized controls subclassed from the Office classes could be written to dump the bitmaps directly into images.  The following is a sample implementation of the first approach.  Don't forget to add the reference to COM object library "Microsoft PointPoint 12.0 Object Library".

Creating Thumbnails for PowerPoint Presentations
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

private void pptThumbnail(string sourceFile, string targetFile, int thumbW, int thumbH)
{
    // Open the document and convert into HTML pages
    PowerPoint.ApplicationClass oApp = new PowerPoint.ApplicationClass();
    PowerPoint.Presentation oDoc = oApp.Presentations.Open(
        sourceFile,
        Microsoft.Office.Core.MsoTriState.msoTrue, // read only
        Microsoft.Office.Core.MsoTriState.msoTrue, // untitled
        Microsoft.Office.Core.MsoTriState.msoFalse); // with window
    string tmpHtmlFile = System.IO.Path.GetTempFileName() + ".html";
    oApp.Presentations[1].SaveCopyAs(
        tmpHtmlFile,
        PowerPoint.PpSaveAsFileType.ppSaveAsHTML, // format
        Microsoft.Office.Core.MsoTriState.msoTrue); // embed true type font

    // Create the thumbnail from the HTML pages
    Size browserSize = new Size(800, 800);
    WebBrowser browser = new WebBrowser();
    browser.Size = browserSize;
    browser.Navigate(tmpHtmlFile);
    while (WebBrowserReadyState.Complete != browser.ReadyState)
    {
        Application.DoEvents();
    }
    Bitmap bm = new Bitmap(browserSize.Width, browserSize.Height);
    browser.DrawToBitmap(bm,
        new Rectangle(0, 0, browserSize.Width, browserSize.Height));
    Bitmap thumbnail = new Bitmap(thumbW, thumbH);
    Graphics g = Graphics.FromImage(thumbnail);
    g.DrawImage(
        bm,
        new Rectangle(0, 0, thumbnail.Width, thumbnail.Height),
        new Rectangle(0, 0, browserSize.Width, browserSize.Height),
        GraphicsUnit.Pixel);
    thumbnail.Save(targetFile);
}

TrackBack

TrackBack URL for this entry:
http://stanblog.jojoyao.com/cgi-bin/mt/mt-tb.cgi/9

Post a comment


About

This page contains a single entry from the blog posted on February 7, 2007 9:53 PM.

The previous post in this blog was Windows XP tip for quicker opening a program from Start menu.

The next post in this blog is Customized search box and live search macro.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33