Notifications
Clear all

[Closed] Get max file Thumbnail

Hello everybady!
The code below gives an alternative possibility to get a thumbnail from a max file. It does not use external libraries. Only the possibilities of a max script.
Try to test who is interested.


rollout maxFileThumb "Drop Box" width:300 height:300
(
   button bFile "Max file"
   dotNetControl bm "System.Windows.Forms.PictureBox"

   local VBClass

   fn VBCompiler =
   (
      codeStr =  ""
      codeStr += "Imports System
"
      codeStr += "Imports System.Drawing
"
      codeStr += "Imports System.Runtime.InteropServices
"
      codeStr += "Imports System.Security
"
      codeStr += "Namespace GetThumbs
"
      codeStr += "<ComImport(), Guid(\"43826d1e-e718-42ee-bc55-a1e261c37bfe\"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> Public Interface IShellItem
"
      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)
"
      codeStr += "Sub GetParent(<Out()> ByRef ppsi As IShellItem)
"
      codeStr += "Sub GetDisplayName(ByVal sigdnName As SIGDN, <Out()> ByRef ppszName As IntPtr)
"
      codeStr += "Sub GetAttributes(ByVal sfgaoMask As UInt32, <Out()> ByRef psfgaoAttribs As UInt32)
"
      codeStr += "Sub [Compare](ByVal psi As IShellItem, ByVal hint As UInt32, <Out()> ByRef piOrder As Integer)
"
      codeStr += "End Interface
"
      codeStr += "<ComImport(), Guid(\"bcc18b79-ba16-442f-80c4-8a59c30c463b\"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> Public Interface IShellItemImageFactory
"
      codeStr += "Sub GetImage(<[In](), MarshalAs(UnmanagedType.Struct)> ByVal size As SIZE, <[In]()> ByVal flags As SIIGBF, <Out()> ByRef phbm As IntPtr)
"
      codeStr += "End Interface
"
      codeStr += "<SuppressUnmanagedCodeSecurity()> Public Class SafeNativeMethods
"
      codeStr += "End Class
"
      codeStr += "Public Enum SIGDN As UInt32
"
      codeStr += "DESKTOPABSOLUTEEDITING = &H8004C000L
"
      codeStr += "DESKTOPABSOLUTEPARSING = &H80028000L
"
      codeStr += "FILESYSPATH = &H80058000L
"
      codeStr += "NORMALDISPLAY = 0
"
      codeStr += "PARENTRELATIVEEDITING = &H80031001L
"
      codeStr += "PARENTRELATIVEFORADDRESSBAR = &H8001C001L
"
      codeStr += "PARENTRELATIVEPARSING = &H80018001L
"
      codeStr += "URL = &H80068000L
"
      codeStr += "End Enum
"
      codeStr += "<Flags()> Public Enum SIIGBF
"
      codeStr += "SIIGBF_BIGGERSIZEOK = 1
"
      codeStr += "SIIGBF_ICONONLY = 4
"
      codeStr += "SIIGBF_INCACHEONLY = &H10
"
      codeStr += "SIIGBF_MEMORYONLY = 2
"
      codeStr += "SIIGBF_RESIZETOFIT = 0
"
      codeStr += "SIIGBF_THUMBNAILONLY = 8
"
      codeStr += "End Enum
"
      codeStr += "<StructLayout(LayoutKind.Sequential)> Public Structure SIZE
"
      codeStr += "Public cx As Integer
"
      codeStr += "Public cy As Integer
"
      codeStr += "Public Sub New(ByVal cx As Integer, ByVal cy As Integer)
"
      codeStr += "Me.cx = cx
"
      codeStr += "Me.cy = cy
"
      codeStr += "End Sub
"
      codeStr += "End Structure
"
      codeStr += "<SuppressUnmanagedCodeSecurity()> Public Class UnsafeNativeMethods
"
      codeStr += "<DllImport(\"shell32.dll\", CharSet:=CharSet.Unicode, PreserveSig:=False)>
"
      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)
"
      codeStr += "End Sub
"
      codeStr += "End Class
"
      codeStr += "Public Class Thumbs
"
      codeStr += "Public Shared Function GenerateThumbnail(ByVal filename As String) As Bitmap
"
      codeStr += "Dim ppsi As IShellItem = Nothing
"
      codeStr += "Dim hbitmap As IntPtr = IntPtr.Zero
"
      codeStr += "Dim uuid As New Guid(\"43826d1e-e718-42ee-bc55-a1e261c37bfe\")
"
      codeStr += "UnsafeNativeMethods.SHCreateItemFromParsingName( filename, IntPtr.Zero, uuid, ppsi)
"
      codeStr += "DirectCast(ppsi, IShellItemImageFactory).GetImage(New SIZE(&H100, &H100), SIIGBF.SIIGBF_RESIZETOFIT, hbitmap)
"
      codeStr += "Dim bs As Bitmap
"
      codeStr += "bs = System.Drawing.Bitmap.FromHbitmap(hbitmap)
"
      codeStr += "Return bs
"
      codeStr += "End Function
"
      codeStr += "End Class
"
      codeStr += "End Namespace
"
      codeStr += "Public Class VBClass
"
      codeStr += "Public Function GetThumbnail(ByVal filename As String) As Bitmap
"
      codeStr += "Return GetThumbs.Thumbs.GenerateThumbnail(filename)
"
      codeStr += "End Function
"
      codeStr += "End Class
"

      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
      (
         img = VBClass.GetThumbnail file
         bm.width = img.width
         bm.height = img.height
         bm.Image = img
      )
   )

)
createDialog maxFileThumb