c# - 获取任何文件的缩略图,包括 Windows XP/Vista 上的 SolidWorks

标签 c# .net api thumbnails solidworks

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

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

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

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

最佳答案

有几种方式:

1) 带图书馆OpenMCDF . Solidworks 文件是 Compound document所以访问它的内容 - 正在解析文件。

 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();
        }

关于c# - 获取任何文件的缩略图,包括 Windows XP/Vista 上的 SolidWorks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11888025/

相关文章:

python - 从json中提取某些数据

c# - 动态 AX 2012 : Dynamically create a class

c# - 获取解决方案本地子文件夹的 TFS 映射文件夹?

c# - 从 XElement 中读取元素与纯内容混合的数据

c# - MVC - 并行任务中的 System.Threading.ThreadAbortException

python - Plotly.js 连接到 Django Rest Framework API - 这可能吗?如果可能的话,如何实现?

c# - Visual Studio 2015 预览版中缺少报告模板

c# - 如何从资源文件中获取流对象(控制台应用程序/Windows 服务项目)

c# - 访问数据库替代品

node.js - 通过 NodeJS 调用远程 API 并将结果附加到我的 API 失败