EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 762|回复: 1
打印 上一主题 下一主题

查看Revit文件缩略图

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12396

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-9 13:24:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查看Revit文件缩略图的方法

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;

  6. using System.Drawing;
  7. using System.Runtime.InteropServices;
  8. using System.Drawing.Imaging;

  9. namespace RevitApp
  10. {
  11.    internal class ThumbnailCreator : IDisposable
  12.    {
  13.       // Fields
  14.       private IMalloc alloc;
  15.       private Size desiredSize;
  16.       private bool disposed;
  17.       private Bitmap thumbnail;

  18.       // Methods
  19.       public ThumbnailCreator()
  20.       {
  21.          this.desiredSize = new Size(128, 128);
  22.       }

  23.       public ThumbnailCreator(int width, int height)
  24.       {
  25.          desiredSize = new Size(128, 128);
  26.          desiredSize.Width = width;
  27.          desiredSize.Height = height;
  28.       }

  29.       public void Dispose()
  30.       {
  31.          if (!this.disposed)
  32.          {
  33.             if (this.alloc != null)
  34.             {
  35.                Marshal.ReleaseComObject(this.alloc);
  36.             }
  37.             this.alloc = null;
  38.             if (this.thumbnail != null)
  39.             {
  40.                this.thumbnail.Dispose();
  41.             }
  42.             this.disposed = true;
  43.          }
  44.       }

  45.       ~ThumbnailCreator()
  46.       {
  47.          this.Dispose();
  48.       }

  49.       private bool getThumbNail(string file, IntPtr pidl, IShellFolder item)
  50.       {
  51.          bool CS;
  52.          IntPtr hBmp = IntPtr.Zero;
  53.          IExtractImage extractImage = null;
  54.          try
  55.          {
  56.             if (Path.GetFileName(PathFromPidl(pidl)).ToUpper().Equals(Path.GetFileName(file).ToUpper()))
  57.             {
  58.                int prgf;
  59.                IUnknown iunk = null;
  60.                Guid iidExtractImage = new Guid(“BB2E617C-0920-11d1-9A0B-00C04FC2D6C1″);
  61.                item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, out prgf, ref iunk);
  62.                extractImage = (IExtractImage)iunk;
  63.                if (extractImage != null)
  64.                {
  65.                   SIZE sz = new SIZE
  66.                   {
  67.                      cx = this.desiredSize.Width,
  68.                      cy = this.desiredSize.Height
  69.                   };
  70.                   StringBuilder location = new StringBuilder(260, 260);
  71.                   int priority = 0;
  72.                   int requestedColourDepth = 0×20;
  73.                   EIEIFLAG flags = EIEIFLAG.IEIFLAG_SCREEN | EIEIFLAG.IEIFLAG_ASPECT;
  74.                   int uFlags = (int)flags;
  75.                   extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags);
  76.                   extractImage.Extract(out hBmp);
  77.                   if (hBmp != IntPtr.Zero)
  78.                   {
  79.                      this.thumbnail = Image.FromHbitmap(hBmp);
  80.                   }
  81.                   Marshal.ReleaseComObject(extractImage);
  82.                   extractImage = null;
  83.                }
  84.                return true;
  85.             }
  86.             CS = false;
  87.          }
  88.          catch (Exception)
  89.          {
  90.             if (hBmp != IntPtr.Zero)
  91.             {
  92.                UnManagedMethods.DeleteObject(hBmp);
  93.             }
  94.             if (extractImage != null)
  95.             {
  96.                Marshal.ReleaseComObject(extractImage);
  97.             }
  98.             throw;
  99.          }
  100.          return CS;
  101.       }

  102.       public Bitmap GetThumbNail(string file)
  103.       {
  104.          if (!File.Exists(file) && !Directory.Exists(file))
  105.          {
  106.             throw new FileNotFoundException(string.Format(“The file ‘{0}’ does not exist”, file), file);
  107.          }
  108.          if (this.thumbnail != null)
  109.          {
  110.             this.thumbnail.Dispose();
  111.             this.thumbnail = null;
  112.          }
  113.          IShellFolder folder = getDesktopFolder;
  114.          if (folder != null)
  115.          {
  116.             IntPtr pidlMain;
  117.             try
  118.             {
  119.                int cParsed;
  120.                int pdwAttrib;
  121.                string filePath = Path.GetDirectoryName(file);
  122.                folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlMain, out pdwAttrib);
  123.             }
  124.             catch (Exception)
  125.             {
  126.                Marshal.ReleaseComObject(folder);
  127.                throw;
  128.             }
  129.             if (pidlMain != IntPtr.Zero)
  130.             {
  131.                Guid iidShellFolder = new Guid(“000214E6-0000-0000-C000-000000000046″);
  132.                IShellFolder item = null;
  133.                try
  134.                {
  135.                   folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item);
  136.                }
  137.                catch (Exception)
  138.                {
  139.                   Marshal.ReleaseComObject(folder);
  140.                   this.Allocator.Free(pidlMain);
  141.                   throw;
  142.                }
  143.                if (item != null)
  144.                {
  145.                   IEnumIDList idEnum = null;
  146.                   try
  147.                   {
  148.                      item.EnumObjects(IntPtr.Zero, ESHCONTF.SHCONTF_NONFOLDERS | ESHCONTF.SHCONTF_FOLDERS, ref idEnum);
  149.                   }
  150.                   catch (Exception)
  151.                   {
  152.                      Marshal.ReleaseComObject(folder);
  153.                      this.Allocator.Free(pidlMain);
  154.                      throw;
  155.                   }
  156.                   if (idEnum != null)
  157.                   {
  158.                      IntPtr pidl = IntPtr.Zero;
  159.                      bool complete = false;
  160.                      while (!complete)
  161.                      {
  162.                         int fetched;
  163.                         if (idEnum.Next(1, ref pidl, out fetched) != 0)
  164.                         {
  165.                            pidl = IntPtr.Zero;
  166.                            complete = true;
  167.                         }
  168.                         else if (this.getThumbNail(file, pidl, item))
  169.                         {
  170.                            complete = true;
  171.                         }
  172.                         if (pidl != IntPtr.Zero)
  173.                         {
  174.                            this.Allocator.Free(pidl);
  175.                         }
  176.                      }
  177.                      Marshal.ReleaseComObject(idEnum);
  178.                   }
  179.                   Marshal.ReleaseComObject(item);
  180.                }
  181.                this.Allocator.Free(pidlMain);
  182.             }
  183.             Marshal.ReleaseComObject(folder);
  184.          }
  185.          return this.thumbnail;
  186.       }

  187.       private static string PathFromPidl(IntPtr pidl)
  188.       {
  189.          StringBuilder path = new StringBuilder(260, 260);
  190.          if (UnManagedMethods.SHGetPathFromIDList(pidl, path) != 0)
  191.          {
  192.             return path.ToString();
  193.          }
  194.          return string.Empty;
  195.       }

  196.       // Properties
  197. #region Properties
  198.       private IMalloc Allocator
  199.       {
  200.          get
  201.          {
  202.             if (!this.disposed && (this.alloc == null))
  203.             {
  204.                UnManagedMethods.SHGetMalloc(out this.alloc);
  205.             }
  206.             return this.alloc;
  207.          }
  208.       }

  209.       public Size DesiredSize
  210.       {
  211.          get
  212.          {
  213.             return this.desiredSize;
  214.          }
  215.          set
  216.          {
  217.             this.desiredSize = value;
  218.          }
  219.       }

  220.       public Bitmap ThumbNail
  221.       {
  222.          get
  223.          {
  224.             return this.thumbnail;
  225.          }
  226.       }

  227.       private static IShellFolder getDesktopFolder
  228.       {
  229.          get
  230.          {
  231.             IShellFolder ppshf;
  232.             UnManagedMethods.SHGetDesktopFolder(out ppshf);
  233.             return ppshf;
  234.          }
  235.       }
  236. #endregion
  237. #region COMDefinitions
  238.       // Nested Types
  239.       private enum EIEIFLAG
  240.       {
  241.          IEIFLAG_ASPECT = 4,
  242.          IEIFLAG_ASYNC = 1,
  243.          IEIFLAG_CACHE = 2,
  244.          IEIFLAG_GLEAM = 0×10,
  245.          IEIFLAG_NOBORDER = 0×100,
  246.          IEIFLAG_NOSTAMP = 0×80,
  247.          IEIFLAG_OFFLINE = 8,
  248.          IEIFLAG_ORIGSIZE = 0×40,
  249.          IEIFLAG_QUALITY = 0×200,
  250.          IEIFLAG_SCREEN = 0×20
  251.       }

  252.       [Flags]
  253.       private enum ESFGAO
  254.       {
  255.          SFGAO_CANCOPY = 1,
  256.          SFGAO_CANDELETE = 0×20,
  257.          SFGAO_CANLINK = 4,
  258.          SFGAO_CANMOVE = 2,
  259.          SFGAO_CANRENAME = 0×10,
  260.          SFGAO_CAPABILITYMASK = 0×177,
  261.          SFGAO_COMPRESSED = 0×4000000,
  262.          SFGAO_CONTENTSMASK = -2147483648,
  263.          SFGAO_DISPLAYATTRMASK = 0xf0000,
  264.          SFGAO_DROPTARGET = 0×100,
  265.          SFGAO_FILESYSANCESTOR = 0×10000000,
  266.          SFGAO_FILESYSTEM = 0×40000000,
  267.          SFGAO_FOLDER = 0×20000000,
  268.          SFGAO_GHOSTED = 0×80000,
  269.          SFGAO_HASPROPSHEET = 0×40,
  270.          SFGAO_HASSUBFOLDER = -2147483648,
  271.          SFGAO_LINK = 0×10000,
  272.          SFGAO_READONLY = 0×40000,
  273.          SFGAO_REMOVABLE = 0×2000000,
  274.          SFGAO_SHARE = 0×20000,
  275.          SFGAO_VALIDATE = 0×1000000
  276.       }

  277.       [Flags]
  278.       private enum ESHCONTF
  279.       {
  280.          SHCONTF_FOLDERS = 0×20,
  281.          SHCONTF_INCLUDEHIDDEN = 0×80,
  282.          SHCONTF_NONFOLDERS = 0×40
  283.       }

  284.       [Flags]
  285.       private enum ESHGDN
  286.       {
  287.          SHGDN_FORADDRESSBAR = 0×4000,
  288.          SHGDN_FORPARSING = 0×8000,
  289.          SHGDN_INFOLDER = 1,
  290.          SHGDN_NORMAL = 0
  291.       }

  292.       [Flags]
  293.       private enum ESTRRET
  294.       {
  295.          STRRET_WSTR,
  296.          STRRET_OFFSET,
  297.          STRRET_CSTR
  298.       }

  299.       [ComImport, Guid("000214F2-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  300.       private interface IEnumIDList
  301.       {
  302.          [PreserveSig]
  303.          int Next(int celt, ref IntPtr rgelt, out int pceltFetched);
  304.          void Skip(int celt);
  305.          void Reset();
  306.          void Clone(ref ThumbnailCreator.IEnumIDList ppenum);
  307.       }

  308.       [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1")]
  309.       private interface IExtractImage
  310.       {
  311.          void GetLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszPathBuffer, int cch, ref int pdwPriority, ref ThumbnailCreator.SIZE prgSize, int dwRecClrDepth, ref int pdwFlags);
  312.          void Extract(out IntPtr phBmpThumbnail);
  313.       }

  314.       [ComImport, Guid("00000002-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  315.       private interface IMalloc
  316.       {
  317.          [PreserveSig]
  318.          IntPtr Alloc(int cb);
  319.          [PreserveSig]
  320.          IntPtr Realloc(IntPtr pv, int cb);
  321.          [PreserveSig]
  322.          void Free(IntPtr pv);
  323.          [PreserveSig]
  324.          int GetSize(IntPtr pv);
  325.          [PreserveSig]
  326.          int DidAlloc(IntPtr pv);
  327.          [PreserveSig]
  328.          void HeapMinimize();
  329.       }

  330.       [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214E6-0000-0000-C000-000000000046")]
  331.       private interface IShellFolder
  332.       {
  333.          void ParseDisplayName(IntPtr hwndOwner, IntPtr pbcReserved, [MarshalAs(UnmanagedType.LPWStr)] string lpszDisplayName, out int pchEaten, out IntPtr ppidl, out int pdwAttributes);
  334.          void EnumObjects(IntPtr hwndOwner, [MarshalAs(UnmanagedType.U4)] ThumbnailCreator.ESHCONTF grfFlags, ref ThumbnailCreator.IEnumIDList ppenumIDList);
  335.          void BindToObject(IntPtr pidl, IntPtr pbcReserved, ref Guid riid, ref ThumbnailCreator.IShellFolder ppvOut);
  336.          void BindToStorage(IntPtr pidl, IntPtr pbcReserved, ref Guid riid, IntPtr ppvObj);
  337.          [PreserveSig]
  338.          int CompareIDs(IntPtr lParam, IntPtr pidl1, IntPtr pidl2);
  339.          void CreateViewObject(IntPtr hwndOwner, ref Guid riid, IntPtr ppvOut);
  340.          void GetAttributesOf(int cidl, IntPtr apidl, [MarshalAs(UnmanagedType.U4)] ref ThumbnailCreator.ESFGAO rgfInOut);
  341.          void GetUIObjectOf(IntPtr hwndOwner, int cidl, ref IntPtr apidl, ref Guid riid, out int prgfInOut, ref ThumbnailCreator.IUnknown ppvOut);
  342.          void GetDisplayNameOf(IntPtr pidl, [MarshalAs(UnmanagedType.U4)] ThumbnailCreator.ESHGDN uFlags, ref ThumbnailCreator.STRRET_CSTR lpName);
  343.          void SetNameOf(IntPtr hwndOwner, IntPtr pidl, [MarshalAs(UnmanagedType.LPWStr)] string lpszName, [MarshalAs(UnmanagedType.U4)] ThumbnailCreator.ESHCONTF uFlags, ref IntPtr ppidlOut);
  344.       }

  345.       [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000000-0000-0000-C000-000000000046")]
  346.       private interface IUnknown
  347.       {
  348.          [PreserveSig]
  349.          IntPtr QueryInterface(ref Guid riid, out IntPtr pVoid);
  350.          [PreserveSig]
  351.          IntPtr AddRef();
  352.          [PreserveSig]
  353.          IntPtr Release();
  354.       }

  355.       [StructLayout(LayoutKind.Sequential)]
  356.       private struct SIZE
  357.       {
  358.          public int cx;
  359.          public int cy;
  360.       }

  361.       [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto)]
  362.       private struct STRRET_ANY
  363.       {
  364.          // Fields
  365.          [FieldOffset(4)]
  366.          public IntPtr pOLEString;
  367.          [FieldOffset(0)]
  368.          public ThumbnailCreator.ESTRRET uType;
  369.       }

  370.       [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)]
  371.       private struct STRRET_CSTR
  372.       {
  373.          public ThumbnailCreator.ESTRRET uType;
  374.          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 520)]
  375.          public byte[] cStr;
  376.       }

  377.       private class UnManagedMethods
  378.       {
  379.          // Methods
  380.          [DllImport("gdi32", CharSet = CharSet.Auto)]
  381.          internal static extern int DeleteObject(IntPtr hObject);
  382.          [DllImport("shell32", CharSet = CharSet.Auto)]
  383.          internal static extern int SHGetDesktopFolder(out ThumbnailCreator.IShellFolder ppshf);
  384.          [DllImport("shell32", CharSet = CharSet.Auto)]
  385.          internal static extern int SHGetMalloc(out ThumbnailCreator.IMalloc ppMalloc);
  386.          [DllImport("shell32", CharSet = CharSet.Auto)]
  387.          internal static extern int SHGetPathFromIDList(IntPtr pidl, StringBuilder pszPath);
  388.       }
  389. #endregion
  390.    }
  391. }
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对

相关帖子

工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言

31

主题

1019

帖子

1897

积分

BIM经理

Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6Rank: 6

积分
1897
2F
发表于 2014-2-20 14:26:35 | 只看该作者
本帖最后由 Aaronyee 于 2015-11-2 19:41 编辑

顶起来………… 本站强荐:185娱乐⑤城.足球⑤真_人.彩票齐全⑤手机可投⑤注任何游戏. 首次开户送10元.首存送58元.信誉绝对保证185.cc
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-9-28 16:18

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表