get thumbnail image by Metafile

By Xiaodong Liang

A couple of blogs below introduce how to get thumbnail of a document.

Accessing Thumbnail Images

Document Thumbnails and Button Icons

Accessing iProperties 

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">&#160;&#160;&#160; <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>      <p style="margin: 0px">}</p>      <p style="margin: 0px">&#160;</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">&#160;&#160;&#160; <span style="color: #2b91af">OpenFileDialog</span> oFDlg = <span style="color: blue">new</span> <span style="color: #2b91af">OpenFileDialog</span>();</p>      <p style="margin: 0px">&#160;&#160;&#160; <span style="color: blue">if</span> (oFDlg.ShowDialog() == System.Windows.Forms.<span style="color: #2b91af">DialogResult</span>.OK)</p>      <p style="margin: 0px">&#160;&#160;&#160; {</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">ApprenticeServerComponent</span> oApprenticeApp = </p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">ApprenticeServerComponent</span>();</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">ApprenticeServerDocument</span> oDoc;</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">try</span></p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; oDoc = oApprenticeApp.Open(oFDlg.FileName);</p>      <p style="margin: 0px">&#160;</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">catch</span> (<span style="color: #2b91af">Exception</span> ex)</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">MessageBox</span>.Show(<span style="color: #a31515">&quot;cannot open the file!&quot;</span>);</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">return</span>;</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>      <p style="margin: 0px">&#160;</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; stdole.<span style="color: #2b91af">IPictureDisp</span> oPD =&#160; oDoc.Thumbnail;</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Metafile</span> oMF = <span style="color: blue">new</span>&#160; <span style="color: #2b91af">Metafile</span>(</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">IntPtr</span>(oPD.Handle),</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span> <span style="color: #2b91af">WmfPlaceableFileHeader</span>());</p>      <p style="margin: 0px">&#160;</p>      <p style="margin: 0px">&#160;</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">Image</span>.<span style="color: #2b91af">GetThumbnailImageAbort</span> oCallBack = </p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">new</span>&#160;&#160; <span style="color: #2b91af">Image</span>.<span style="color: #2b91af">GetThumbnailImageAbort</span>(ThumbnailCallback);</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; pictureBox1.Image = </p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; oMF.GetThumbnailImage(pictureBox1.Width, </p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; pictureBox1.Height,</p>      <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; oCallBack, <span style="color: #2b91af">IntPtr</span>.Zero);</p>      <p style="margin: 0px">&#160;</p>      <p style="margin: 0px">&#160;&#160;&#160; }</p>   </div>    <p style="margin: 0px">&#160; <p></p>      <p>&#160;</p>   </p> </div>

Comments

2 responses to “get thumbnail image by Metafile”

  1. WONDERFUL!
    This is actually the only wonking solution to take a snapshot of a thumbnail of a Inventor file and convert it into bitmap!
    It works on a virtualized windows 7 @ 64 bit!
    Thank you very much Xiaodong Liang!

  2. For everyone with my same problem, i post my VBNET code (based on this fantastic post) to extract an image from a inventor *.ipt, *iam, etc file:
    (… code …)
    Dim m_App As Inventor.Application
    m_App = CType(GetObject(, “Inventor.Application”), Inventor.Application)
    Dim InventorDoc As Document = m_App.ActiveDocument
    Dim image As Image = Nothing
    Dim imgThumb As Bitmap = Nothing
    ‘inventor thumbnail wrapping
    Dim oThumb As stdole.IPictureDisp = m_App.ActiveDocument.Thumbnail
    Dim oMF As New Metafile(New IntPtr(oThumb.Handle), New WmfPlaceableFileHeader())
    Dim oCallBack As New Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
    Dim org As Image = oMF.GetThumbnailImage(400, 400, oCallBack, IntPtr.Zero)
    ‘re-draw a color bitmap
    imgThumb = New Bitmap(org.Width, org.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
    imgThumb.SetResolution(org.HorizontalResolution, org.VerticalResolution)
    Dim g As Graphics = Graphics.FromImage(imgThumb)
    g.DrawImage(org, 0, 0)
    g.Dispose()
    ”now in imgThumb you have your image ready fo a picturebox or ”other

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading