A couple of blogs below introduce how to get thumbnail of a document.
Document Thumbnails and Button Icons
In this post, we use one more way which takes advantage of Metafile of System.Drawing.Imaging.
The code assumes there is one picturebox and one button in one Windows form application. When the button clicks, a file dialog will ask you to select an Inventor file, and the picturebox will display its thumbnail.
using Inventor; using System.Windows.Forms; using System.Drawing; using System.Drawing.Imaging; using stdole;
<p style="margin: 0px"><span style="color: green"></span></p> <p style="margin: 0px"><span style="color: green">// callback function for GetThumbnailImageAbort</span></p> <p style="margin: 0px"><span style="color: blue">bool</span> ThumbnailCallback()</p> <p style="margin: 0px">{</p> <p style="margin: 0px">    <span style="color: blue">return</span> <span style="color: blue">false</span>;</p> <p style="margin: 0px">}</p> <p style="margin: 0px"> </p> <p style="margin: 0px"><span style="color: green">// button event to get thumbnail</span></p> <p style="margin: 0px"><span style="color: blue">private</span> <span style="color: blue">void</span> btnOpen_Click(<span style="color: blue">object</span> sender, <span style="color: #2b91af">EventArgs</span> e)</p> <p style="margin: 0px">{</p> <p style="margin: 0px">    <span style="color: #2b91af">OpenFileDialog</span> oFDlg = <span style="color: blue">new</span> <span style="color: #2b91af">OpenFileDialog</span>();</p> <p style="margin: 0px">    <span style="color: blue">if</span> (oFDlg.ShowDialog() == System.Windows.Forms.<span style="color: #2b91af">DialogResult</span>.OK)</p> <p style="margin: 0px">    {</p> <p style="margin: 0px">        <span style="color: #2b91af">ApprenticeServerComponent</span> oApprenticeApp = </p> <p style="margin: 0px">            <span style="color: blue">new</span> <span style="color: #2b91af">ApprenticeServerComponent</span>();</p> <p style="margin: 0px">        <span style="color: #2b91af">ApprenticeServerDocument</span> oDoc;</p> <p style="margin: 0px">        <span style="color: blue">try</span></p> <p style="margin: 0px">        {</p> <p style="margin: 0px">            oDoc = oApprenticeApp.Open(oFDlg.FileName);</p> <p style="margin: 0px"> </p> <p style="margin: 0px">        }</p> <p style="margin: 0px">        <span style="color: blue">catch</span> (<span style="color: #2b91af">Exception</span> ex)</p> <p style="margin: 0px">        {</p> <p style="margin: 0px">            <span style="color: #2b91af">MessageBox</span>.Show(<span style="color: #a31515">"cannot open the file!"</span>);</p> <p style="margin: 0px">            <span style="color: blue">return</span>;</p> <p style="margin: 0px">        }</p> <p style="margin: 0px"> </p> <p style="margin: 0px">        stdole.<span style="color: #2b91af">IPictureDisp</span> oPD =  oDoc.Thumbnail;</p> <p style="margin: 0px">        <span style="color: #2b91af">Metafile</span> oMF = <span style="color: blue">new</span>  <span style="color: #2b91af">Metafile</span>(</p> <p style="margin: 0px">            <span style="color: blue">new</span> <span style="color: #2b91af">IntPtr</span>(oPD.Handle),</p> <p style="margin: 0px">            <span style="color: blue">new</span> <span style="color: #2b91af">WmfPlaceableFileHeader</span>());</p> <p style="margin: 0px"> </p> <p style="margin: 0px"> </p> <p style="margin: 0px">        <span style="color: #2b91af">Image</span>.<span style="color: #2b91af">GetThumbnailImageAbort</span> oCallBack = </p> <p style="margin: 0px">            <span style="color: blue">new</span>   <span style="color: #2b91af">Image</span>.<span style="color: #2b91af">GetThumbnailImageAbort</span>(ThumbnailCallback);</p> <p style="margin: 0px">            pictureBox1.Image = </p> <p style="margin: 0px">            oMF.GetThumbnailImage(pictureBox1.Width, </p> <p style="margin: 0px">                                    pictureBox1.Height,</p> <p style="margin: 0px">                                    oCallBack, <span style="color: #2b91af">IntPtr</span>.Zero);</p> <p style="margin: 0px"> </p> <p style="margin: 0px">    }</p> </div> <p style="margin: 0px">  <p></p> <p> </p> </p> </div>

Leave a Reply to DaveCancel reply