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".
| 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); } |