Get max file Thumbnail

How to retrieve the thumbnail from max file without any external libraries.

rollout maxFileThumb "ShowThumb" width:280 height:220
   button bFile "Max file"
   label lblFile ""
   dotNetControl bm "System.Windows.Forms.PictureBox"
   local VBClass
   fn VBCompiler =
      codeStr =  ""
      codeStr += "Imports System\n"
      codeStr += "Imports System.Drawing\n"
      codeStr += "Imports System.Runtime.InteropServices\n"
      codeStr += "Imports System.Security\n"
      codeStr += "Namespace GetThumbs\n"
      codeStr += "<ComImport(), Guid(\"43826d1e-e718-42ee-bc55-a1e261c37bfe\"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> Public Interface IShellItem\n"
      codeStr += "Sub BindToHandler(ByVal pbc As IntPtr, <MarshalAs(UnmanagedType.LPStruct)> ByVal bhid As Guid, <MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid, <Out()> ByRef ppv As IntPtr)\n"
      codeStr += "Sub GetParent(<Out()> ByRef ppsi As IShellItem)\n"
      codeStr += "Sub GetDisplayName(ByVal sigdnName As SIGDN, <Out()> ByRef ppszName As IntPtr)\n"
      codeStr += "Sub GetAttributes(ByVal sfgaoMask As UInt32, <Out()> ByRef psfgaoAttribs As UInt32)\n"
      codeStr += "Sub [Compare](ByVal psi As IShellItem, ByVal hint As UInt32, <Out()> ByRef piOrder As Integer)\n"
      codeStr += "End Interface\n"
      codeStr += "<ComImport(), Guid(\"bcc18b79-ba16-442f-80c4-8a59c30c463b\"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown  )> Public Interface IShellItemImageFactory\n"
      codeStr += "Sub GetImage(<[In](), MarshalAs(UnmanagedType.Struct)> ByVal size As SIZE, <[In]()> ByVal flags As SIIGBF, <Out()> ByRef phbm As IntPtr)\n"
      codeStr += "End Interface\n"
      codeStr += "<SuppressUnmanagedCodeSecurity()> Public Class SafeNativeMethods\n"
      codeStr += "End Class\n"
      codeStr += "Public Enum SIGDN As UInt32\n"
      codeStr += "DESKTOPABSOLUTEEDITING = &H8004C000L\n"
      codeStr += "DESKTOPABSOLUTEPARSING = &H80028000L\n"
      codeStr += "FILESYSPATH = &H80058000L\n"
      codeStr += "NORMALDISPLAY = 0\n"
      codeStr += "PARENTRELATIVEEDITING = &H80031001L\n"
      codeStr += "PARENTRELATIVEFORADDRESSBAR = &H8001C001L\n"
      codeStr += "PARENTRELATIVEPARSING = &H80018001L\n"
      codeStr += "URL = &H80068000L\n"
      codeStr += "End Enum\n"
      codeStr += "<Flags()> Public Enum SIIGBF\n"
      codeStr += "SIIGBF_BIGGERSIZEOK = 1\n"
      codeStr += "SIIGBF_ICONONLY = 4\n"
      codeStr += "SIIGBF_INCACHEONLY = &H10\n"
      codeStr += "SIIGBF_MEMORYONLY = 2\n"
      codeStr += "SIIGBF_RESIZETOFIT = 0\n"
      codeStr += "SIIGBF_THUMBNAILONLY = 8\n"
      codeStr += "End Enum\n"
      codeStr += "<StructLayout(LayoutKind.Sequential)> Public Structure SIZE\n"
      codeStr += "Public cx As Integer\n"
      codeStr += "Public cy As Integer\n"
      codeStr += "Public Sub New(ByVal cx As Integer, ByVal cy As Integer)\n"
      codeStr += " = cx\n"
      codeStr += " = cy\n"
      codeStr += "End Sub\n"
      codeStr += "End Structure\n"
      codeStr += "<SuppressUnmanagedCodeSecurity()> Public Class UnsafeNativeMethods\n"
      codeStr += "<DllImport(\"shell32.dll\", CharSet:=CharSet.Unicode, PreserveSig:=False)>\n"
      codeStr += "Public Shared Sub SHCreateItemFromParsingName(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pszPath As String, <[In]()> ByVal pbc As IntPtr, <[In](), MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid, <Out()> ByRef ppv As IShellItem)\n"
      codeStr += "End Sub\n"
      codeStr += "End Class\n"
      codeStr += "Public Class Thumbs\n"
      codeStr += "Public Shared Function GenerateThumbnail(ByVal filename As String) As Bitmap\n"
      codeStr += "Dim ppsi As IShellItem = Nothing\n"
      codeStr += "Dim hbitmap As IntPtr = IntPtr.Zero\n"
      codeStr += "Dim uuid As New Guid(\"43826d1e-e718-42ee-bc55-a1e261c37bfe\")\n"
      codeStr += "UnsafeNativeMethods.SHCreateItemFromParsingName(filename, IntPtr.Zero, uuid, ppsi)\n"
      codeStr += "DirectCast(ppsi, IShellItemImageFactory).GetImage(New SIZE(&H100, &H100), SIIGBF.SIIGBF_RESIZETOFIT, hbitmap)\n"
      codeStr += "Dim bs As Bitmap\n"
      codeStr += "bs = System.Drawing.Bitmap.FromHbitmap(hbitmap)\n"
      codeStr += "Return bs\n"
      codeStr += "End Function\n"
      codeStr += "End Class\n"
      codeStr += "End Namespace\n"
      codeStr += "Public Class VBClass\n"
      codeStr += "Public Function GetThumbnail(ByVal filename As String) As Bitmap\n"
      codeStr += "Return GetThumbs.Thumbs.GenerateThumbnail(filename)\n"
      codeStr += "End Function\n"
      codeStr += "End Class\n"
      VBProvider = dotnetobject "Microsoft.VisualBasic.VBCodeProvider"
      compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
      compilerParams.ReferencedAssemblies.add ("C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.drawing.dll")
      compilerParams.GenerateInMemory = on
      compilerResults = VBProvider.CompileAssemblyFromSource compilerParams #(codeStr)
      return compilerResults.CompiledAssembly.CreateInstance "VBClass"
   on maxFileThumb open do
      VBClass = VBCompiler()
   on bFile pressed do
      file = getOpenFileName()
      if file != undefined do
         lblFile.text = file
         img = VBClass.GetThumbnail file
         bm.width = img.width
         bm.height = img.height
         bm.Image = img
createDialog maxFileThumb


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
miauu's picture


Post this as a script - Contribute Content - 3ds Max and so on.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.