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

每个安装的操作系统中都有很多内置的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上是否有任何其他可用的方法获取注册缩略图提供程序的任何文件缩略图?我真的很绝望……

解决方法

有几种方法

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作为控件添加并放置到Form中.

OpenFileDialog dialog = new OpenFileDialog();
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            axEModelViewControl1.OpenDoc(dialog.FileName.ToString(),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,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();
        }

相关文章

在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题。比...
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL)。 业务流程:选择照片...
var array = new byte[4]; var i = Encoding.UTF8.GetBytes(100.ToString("x2"));//...
其实很简单,因为Combox的Item是一个K/V的object,那么就可以把它的items转换成IEnumerable<Dic...
把.net4.6安装包打包进安装程序。 关键脚本如下: 头部引用字符串对比库 !include "WordFunc....
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.NetʿreeSp...