Get thumbnail of any file including SolidWorks on Windows XP/Vista
Asked Answered
O

2

8

There is a lot of build-in ThumbnailProviders inside every installed OS. Due to these providers Windows is able to show Thumbnail of many files. For example Windows Explorer can show content of *.jpg files, but from Solidworks *.sldprt files too (If SolidWorks is installed).

But is there any way to get these thumbnails? I´ve tried to manage this using Windows API CodecPack, but I succeeded only on Windows 7.

ShellFile shellFile = ShellFile.FromFilePath(filePath);                
Bitmap shellThumb = shellFile.Thumbnail.Bitmap;

Question is: is there any other usable way to get Thumbnail of any file with registered Thumbnail provider on Windows XP/Vista? I´m really desperate...

Oversweet answered 9/8, 2012 at 16:39 Comment(0)
C
5

There are several ways:

1) With library OpenMCDF. Solidworks file is Compound document so access to its content - is parsing the file.

 OpenFileDialog dialog = new OpenFileDialog();    
 dialog.InitialDirectory = Application.StartupPath;  
 if (dialog.ShowDialog() == DialogResult.OK)  
 {  
     string STORAGE_NAME = dialog.FileName.ToString();  
     CompoundFile cf = new CompoundFile(STORAGE_NAME);  
     CFStream st = cf.RootStorage.GetStream("PreviewPNG");  
     byte[] buffer = st.GetData();  
     var ms = new MemoryStream(buffer.ToArray());  
     pictureBox1.Image = Image.FromStream(ms);  
  }  

2) With library EModelView.dll to be added as a control and placed to the Form.

    OpenFileDialog dialog = new OpenFileDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            axEModelViewControl1.OpenDoc(dialog.FileName.ToString(), false, false, true, "");
        }

3) With SWExplorer library (wpfPreviewFlowControl)

        OpenFileDialog dialog = new OpenFileDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            string sDocFileName = dialog.FileName.ToString();
            wpfThumbnailCreator pvf;
            pvf = new wpfThumbnailCreator();
            System.Drawing.Size size = new Size();
            size.Width = 200;
            size.Height = 200;
            pvf.DesiredSize = size;
            System.Drawing.Bitmap pic = pvf.GetThumbNail(sDocFileName);
            pictureBox1.Image = pic;
        }

3) With library Document Manager (SolidWorks.Interop.swdocumentmgr)

         OpenFileDialog dialog = new OpenFileDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            string sDocFileName = dialog.FileName.ToString();
            SwDMClassFactory swClassFact = default(SwDMClassFactory);
            SwDMApplication swDocMgr = default(SwDMApplication);
            SwDMDocument swDoc = default(SwDMDocument);
            SwDMConfigurationMgr swCfgMgr = default(SwDMConfigurationMgr);
            string[] vCfgNameArr = null;
            SwDMConfiguration7 swCfg = default(SwDMConfiguration7);
            IPictureDisp pPreview = default(IPictureDisp);
            SwDmDocumentType nDocType = 0;
            SwDmDocumentOpenError nRetVal = 0;
            SwDmPreviewError nRetVal2 = 0;
            Image image = default(Image);

            //Access to interface
            swClassFact = new SwDMClassFactory();
            swDocMgr = (SwDMApplication)swClassFact.GetApplication("Post your code here");
            swDoc = (SwDMDocument)swDocMgr.GetDocument(sDocFileName, nDocType, false, out nRetVal);
            Debug.Assert(SwDmDocumentOpenError.swDmDocumentOpenErrorNone == nRetVal);
            swCfgMgr = swDoc.ConfigurationManager;

            pathLabel.Text = "Path to file: " + swDoc.FullName;
            configLabel.Text = "Active config: " + swCfgMgr.GetActiveConfigurationName();
            vCfgNameArr = (string[])swCfgMgr.GetConfigurationNames();

            foreach (string vCfgName in vCfgNameArr)
            {
                //get preview
                swCfg = (SwDMConfiguration7)swCfgMgr.GetConfigurationByName(vCfgName);
                pPreview = (IPictureDisp)swCfg.GetPreviewPNGBitmap(out nRetVal2);
                image = Support.IPictureDispToImage(pPreview);
                //insert to picturebox
                pictureBox1.BackgroundImage = image;
            }
            swDoc.CloseDoc();
        }
Cyrilcyrill answered 18/1, 2013 at 13:14 Comment(3)
The openMCDF example does not work. There is a "Preview" element but .net tells me it is not an image???Superpatriot
I pointed to the example of solidworks, so should work. If you get a picture for solidworks file, the member name must be "PreviewPNG", not "Preview".Cyrilcyrill
Tried openMCDF, didn't work. Then tried swdocumentmgr. After having hard time finding stdole and microsoft.visualbasic.compatibility dlls, it didn't work as well. It's failing on "GetPreviewPNGBitmap" saying the class doesn't have a usage license.Thorman
S
-1

You can use unmanaged windows shell methods to get the thumbnail

Here is the code (not a small one)

But results are far from perfect.

  • debugging is very hard, unspecified error are common
  • there has to be specific file reader availible on target machine (e.g. pdf-reader for pdfs, didn't tried it on SolidWorks)
  • can work only in Windows
  • performance issue
  • thumbnails low quality (tried it with pdf)
Synergism answered 7/1, 2013 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.