7

每个安装的操作系统中都有很多内置的 ThumbnailProviders。由于这些提供程序,Windows 能够显示许多文件的缩略图。例如,Windows 资源管理器可以显示 *.jpg 文件的内容,但也可以显示 Solidworks *.sldprt 文件的内容(如果安装了 SolidWorks)。

但是有没有办法得到这些缩略图?我尝试使用 Windows API CodecPack 来管理它,但我只在 Windows 7 上成功了。

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

问题是:在 Windows XP/Vista 上,是否有任何其他可用的方法可以通过注册的缩略图提供程序获取任何文件的缩略图?我真的很绝望...

4

2 回答 2

5

有几种方法:

1) 使用库OpenMCDF。Solidworks 文件是复合文档,因此访问其内容 - 正在解析文件。

 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) 将库 EModelView.dll 作为控件添加并放置到窗体中。

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

3) 使用 SWExplorer 库 (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) 带库文档管理器 (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();
        }
于 2013-01-18T13:14:41.900 回答
-1

您可以使用非托管 windows shell 方法来获取缩略图

这是代码(不是小代码)

但结果远非完美。

  • 调试非常困难,未指明的错误很常见
  • 目标机器上必须有特定的文件阅读器可用(例如 pdf-reader for pdfs,没有在 SolidWorks 上尝试过)
  • 只能在 Windows 中工作
  • 性能问题
  • 缩略图质量低(用 pdf 试过)
于 2013-01-07T19:42:36.963 回答