Windows资源管理器可以显示文件的缩略图.这些缩略图由核心和第三方shell扩展提供.
我知道如何扩展shell以向Windows提供缩略图.
我想要做的是使用C#通过shell从系统上的任何文件中检索缩略图.这可能吗?
本质上,我正在编写一个自定义文件浏览器,我想显示缩略图,并且无法解析地球上的每个文件以制作我自己的缩略图.
澄清:许多答案似乎都围绕网页缩略图或缩放图像.但这根本不是我想要的.我想要的是向Windows询问这些文件类型的缩略图:.DOC,.PDF,.3DM,.DWG ......和mabye大约十几个.我不想自己解析,渲染和制作缩略图,因为Windows已经知道如何.
我作为答案发布的代码实际上有用......也许它可以简化并清理一下.
Chr*_*ato 76
今天跑过这个 - 它已经有几个月了,但它完成了我的工作(在Win7上,在MPEG-4文件上提取缩略图):
码:
ShellFile shellFile = ShellFile.FromFilePath(pathToYourFile);
Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
AMi*_*ico 18
SharePoint中的Microsoft Office缩略图,网址为http://msdn.microsoft.com/en-us/library/aa289172(VS.71).aspx.正是你想要的.(我使用 http://www.developerfusion.com/tools/convert/vb-to-csharp/将VB.NET代码转换为C#没有问题.)
关于您在答案中发布的代码,在http://www.vbaccelerator.com/home/net/code/libraries/Shell_Projects/Thumbnail_Extraction/article.asp中使用Shell进行缩略图提取是代码来自的原始文章.我相信几乎所有的样本(你找到的搜索)都是基于这篇文章的代码,因为命名约定,注释等都是从原始代码中提取的.由于本文发布于2003年4月,因此它带有一些非标准(非.NET)编码约定.我做了一些基本测试,还有垃圾收集问题,解决了非托管资源问题以及其他清理问题.因此,我强烈建议避免该文章中的代码.而且,代码的结构是一种使维护变得困难的方法.IExtractImage
2005年7月在MSDN上有一个简洁的简化版本,在SharePoint中称为Microsoft Office Thumbnails,网址为http://msdn.microsoft.com/en-us/library/aa289172(VS.71).aspx.此代码和文章的代码共享相似之处,这使我相信使用Shell提取缩略图文章是SharePoint文章的基础.VB.NET版本GetThumbnailImage
忽略该longestEdge
参数,但C++版本使用它并记录ORIGSIZE
和QUALITY
标志的使用.此外,代码说明了如何使用.NET FreeCoTaskMem
而不是Shell IMalloc
和SHGetMalloc
.
IExtractImage
适用于文件,文件夹和其他命名空间对象.MSDN代码使用隐藏文件,而vbAccelerator代码则需要SHCONTF_INCLUDEHIDDEN
添加到EnumObjects
调用中.此外,vbAccelerator枚举shell文件夹的对象以查找指定的文件,这似乎是浪费时间.可能需要这样才能找到用于GetUIObjectOf
呼叫的正确"相对"PIDL .
完成示例项目,网址为http://cid-7178d2c79ba0a7e3.office.live.com/self.aspx/.Public/ShellThumbnail.zip.
Imports System.Runtime.InteropServices
Namespace Shell
''' <summary>
''' Generates a thumbnail of a folder's picture or a file's image.
''' </summary>
''' <remarks>This is the "Folder's Picture" and not the "Folder's Icon"! Use SHGetFileInfo to generate the thumbnail for a folder's icon or for a file that does not have a thumbnail handler.</remarks>
''' <reference>Microsoft Office Thumbnails in SharePoint at http://msdn.microsoft.com/en-us/library/aa289172%28VS.71%29.aspx.</reference>
Public Class ShellThumbnail
'TODO - Work out the details for image size and IEIFLAG handling.
#Region " Determining Thumbnail Size and Quality [documentation] "
'http://support.microsoft.com/kb/835823
'Determining Thumbnail Size and Quality
'Browse to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer. Create or modify two DWORDs called ThumbnailSize and ThumbnailQuality. For ThumbnailSize set the value in pixels, with the default being 96. For ThumbnailQuality set the value as a number that represents the percentage quality between 50 and 100.
'http://www.pctools.com/guides/registry/detail/1066/ (modified)
' User Key: [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer]
'System Key: [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer]
'Value Name: ThumbnailSize, ThumbnailQuality
' Data Type: REG_DWORD (DWORD Value)
'Value Data: Size in pixels (32-255), Quality Percentage (50-100)
'Microsoft® Windows® XP Registry Guide
'Jerry Honeycutt
'09/11/2002
'Microsoft Press
'http://www.microsoft.com/mspress/books/sampchap/6232.aspx#118
'<H3><I><A name=118></A>Thumbnails</I></H3>The Thumbnails category controls the
'quality of thumbnails in Windows Explorer. Table 5-10 describes the values for
'Image Quality and Size. Create values that you don't see in the registry. The
'default value for <CODE>ThumbnailQuality</CODE> is <CODE>0x5A</CODE>. The
'default value for <CODE>ThumbnailSize</CODE> is <CODE>0x60</CODE>. Keep in mind
'that higher quality and larger thumbnails require more disk space, which is not
'usually a problem, but they also take longer to display. Changing the quality
'does not affect thumbnails that already exist on the file system.
'<P><B>Table 5-10 </B><I>Values in Thumbnails</I>
'<P>
'<TABLE border=0 cellSpacing=1 cellPadding=4 width="100%">
'<TBODY>
'<TR>
'<TD bgColor=#999999 vAlign=top><B>Setting</B></TD>
'<TD bgColor=#999999 vAlign=top><B>Name</B></TD>
'<TD bgColor=#999999 vAlign=top><B>Type</B></TD>
'<TD bgColor=#999999 vAlign=top><B>Data</B></TD></TR>
'<TR>
'<TD bgColor=#cccccc
'vAlign=top><CODE><B>HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer</B></CODE></TD>
'<TD bgColor=#cccccc vAlign=top> </TD>
'<TD bgColor=#cccccc vAlign=top> </TD>
'<TD bgColor=#cccccc vAlign=top> </TD></TR>
'<TR>
'<TD bgColor=#cccccc vAlign=top><CODE>Image Quality</CODE></TD>
'<TD bgColor=#cccccc vAlign=top><CODE>ThumbnailQuality</CODE></TD>
'<TD bgColor=#cccccc vAlign=top><CODE>REG_DWORD</CODE></TD>
'<TD bgColor=#cccccc vAlign=top><CODE>0x32 - 0x64</CODE></TD></TR>
'<TR>
'<TD bgColor=#cccccc vAlign=top><CODE>Size (pixels)</CODE></TD>
'<TD bgColor=#cccccc vAlign=top><CODE>ThumbnailSize</CODE></TD>
'<TD bgColor=#cccccc vAlign=top><CODE>REG_DWORD</CODE></TD>
'<TD bgColor=#cccccc vAlign=top><CODE>0x20 - 0xFF</CODE></TD></TR></TBODY></TABLE></P>
#End Region
Public Shared ReadOnly DefaultThumbnailSize As New System.Drawing.Size(96, 96)
Public Const DefaultColorDepth As Integer = 32
''' <summary>
''' Used to request an image from an object, such as an item in a Shell folder.
''' </summary>
''' <param name="path">An absolute path to a file or folder.</param>
Public Shared Function ExtractImage(ByVal path As String) As Bitmap
Return ExtractImage(path, System.Drawing.Size.Empty, DefaultColorDepth, 0)
End Function
''' <summary>
''' Used to request an image from an object, such as an item in a Shell folder.
''' </summary>
''' <param name="path">An absolute path to a file or folder.</param>
''' <param name="size"></param>
Public Shared Function ExtractImage(ByVal path As String, ByVal size As System.Drawing.Size) As Bitmap
Return ExtractImage(path, size, DefaultColorDepth, 0)
End Function
''' <summary>
''' Used to request an image from an object, such as an item in a Shell folder.
''' </summary>
''' <param name="path">An absolute path to a file or folder.</param>
''' <param name="size"></param>
''' <param name="recommendedColorDepth">The recommended color depth in units of bits per pixel. The default is 32.</param>
Public Shared Function ExtractImage(ByVal path As String, ByVal size As System.Drawing.Size, ByVal recommendedColorDepth As Integer) As Bitmap
Return ExtractImage(path, size, recommendedColorDepth, 0)
End Function
''' <summary>
''' Used to request an image from an object, such as an item in a Shell folder.
''' </summary>
''' <param name="path">An absolute path to a file or folder.</param>
''' <param name="size"></param>
''' <param name="recommendedColorDepth">The recommended color depth in units of bits per pixel. The default is 32.</param>
Private Shared Function ExtractImage(ByVal path As String, ByVal size As System.Drawing.Size, ByVal recommendedColorDepth As Integer, ByVal flags As IEIFLAG) As Bitmap
Dim oResult As Bitmap = Nothing
Dim oDesktopFolder As IShellFolder = Nothing
Dim hParentIDL As IntPtr
Dim hIDL As IntPtr
Dim oParentFolder As IShellFolder
Dim hParentFolder As IntPtr
Dim oExtractImage As IExtractImage
Dim hExtractImage As IntPtr
'Divide the file name into a path and file/folder name.
Dim sFolderName As String = System.IO.Path.GetDirectoryName(path)
Dim sBaseName As String = System.IO.Path.GetFileName(path)
'Get the desktop IShellFolder.
If SHGetDesktopFolder(oDesktopFolder) <> Missico.Win32.S_OK Then
Throw New System.Runtime.InteropServices.COMException
End If
'Get the parent folder for the specified path.
oDesktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, sFolderName, 0, hParentIDL, 0)
oDesktopFolder.BindToObject(hParentIDL, IntPtr.Zero, ShellGUIDs.IID_IShellFolder, hParentFolder)
oParentFolder = CType(Marshal.GetTypedObjectForIUnknown(hParentFolder, GetType(IShellFolder)), IShellFolder)
'Get the file/folder's IExtractImage
oParentFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, sBaseName, 0, hIDL, 0)
oParentFolder.GetUIObjectOf(IntPtr.Zero, 1, New IntPtr() {hIDL}, ShellGUIDs.IID_IExtractImage, IntPtr.Zero, hExtractImage)
'Free the pidls. The Runtime Callable Wrappers (RCW) should automatically release the COM objects.
Marshal.FreeCoTaskMem(hParentIDL)
Marshal.FreeCoTaskMem(hIDL)
Marshal.FinalReleaseComObject(oParentFolder)
Marshal.FinalReleaseComObject(oDesktopFolder)
If hExtractImage = IntPtr.Zero Then
'There is no handler for this file, which is odd. I believe we should default the file's type icon.
Debug.WriteLine(String.Format("There is no thumbnail for the specified file '{0}'.", path), "ShellThumbnail.ExtractImage")
Else
oExtractImage = CType(Marshal.GetTypedObjectForIUnknown(hExtractImage, GetType(IExtractImage)), IExtractImage)
'Set the size and flags
Dim oSize As Missico.Win32.SIZE 'must specify a size
Dim iFlags As IEIFLAG = flags Or IEIFLAG.IEIFLAG_ORIGSIZE Or IEIFLAG.IEIFLAG_QUALITY Or IEIFLAG.IEIFLAG_ASPECT
If size.IsEmpty Then
oSize.cx = DefaultThumbnailSize.Width
oSize.cy = DefaultThumbnailSize.Height
Else
oSize.cx = size.Width
oSize.cy = size.Height
End If
Dim hBitmap As IntPtr
Dim sPath As New System.Text.StringBuilder(Missico.Win32.MAX_PATH, Missico.Win32.MAX_PATH)
oExtractImage.GetLocation(sPath, sPath.Capacity, 0, oSize, recommendedColorDepth, iFlags)
'if the specified path is to a folder then IExtractImage.Extract fails.
Try
oExtractImage.Extract(hBitmap)
Catch ex As System.Runtime.InteropServices.COMException
'clear the handle since extract failed
hBitmap = IntPtr.Zero
Debug.WriteLine(String.Format("There is no thumbnail for the specified folder '{0}'.", path), "ShellThumbnail.ExtractImage")
Finally
Marshal.FinalReleaseComObject(oExtractImage)
End Try
If Not hBitmap.Equals(IntPtr.Zero) Then
'create the image from the handle
oResult = System.Drawing.Bitmap.FromHbitmap(hBitmap)
'dump the properties to determine what kind of bitmap is returned
'Missico.Diagnostics.ObjectDumper.Write(oResult)
'Tag={ }
'PhysicalDimension={Width=96, Height=96}
'Size={Width=96, Height=96}
'Width=96
'Height=96
'HorizontalResolution=96
'VerticalResolution=96
'Flags=335888
'RawFormat={ }
'PixelFormat=Format32bppRgb
'Palette={ }
'FrameDimensionsList=...
'PropertyIdList=...
'PropertyItems=...
Missico.Win32.DeleteObject(hBitmap) 'release the handle
End If
End If
Return oResult
End Function
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
这可能有所帮助:
这包括Shell API,并允许您几乎完成shell的所有操作(包括渲染缩略图).