Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
97 views5 pages

Thumbnails CSharp

The class contains methods for extracting thumbnail images from files. It uses Windows APIs and interfaces like IShellFolder and IExtractImage to retrieve metadata about files and generate thumbnail images. Key methods include ExtractThumbnail which takes a FileInfo object and size and returns a bitmap, and supporting private methods for parsing folder paths, binding to shell objects, and extracting images.

Uploaded by

Rajkumar Shakya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views5 pages

Thumbnails CSharp

The class contains methods for extracting thumbnail images from files. It uses Windows APIs and interfaces like IShellFolder and IExtractImage to retrieve metadata about files and generate thumbnail images. Key methods include ExtractThumbnail which takes a FileInfo object and size and returns a bitmap, and supporting private methods for parsing folder paths, binding to shell objects, and extracting images.

Uploaded by

Rajkumar Shakya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

using using using using using using using using using using

Microsoft.VisualBasic; System; System.Collections; System.Collections.Generic; System.Data; System.Diagnostics; System.IO; System.Drawing; system.Runtime.InteropServices; System.Text;

internal class Thumbnails { private static readonly Guid IID_ISHELLFOLDER = new Guid("000214E6-00000000-C000-000000000046"); private static readonly Guid IID_IEXTRACTIMAGE = new Guid("BB2E617C-0920 -11d1-9A0B-00C04FC2D6C1"); public static Bitmap ExtractThumbNail(FileInfo file) { return ExtractThumbNail(file, new System.Drawing.Size(100, 100)) ; } public static Bitmap ExtractThumbNail(FileInfo file, Size imageSize) { Bitmap thumbnail = null; //Dim alloc As IMalloc = Nothing IShellFolder folder = null; IShellFolder item = null; IntPtr pidlFolder = IntPtr.Zero; IntPtr hBmp = IntPtr.Zero; IExtractImage extractImage = null; IntPtr pidl = IntPtr.Zero; if ((file.Exists)) { try { SHGetDesktopFolder(ref folder); if ((folder != null)) { int cParsed = 0; int pdwAttrib = 0; int HR = folder.ParseDisplayName(IntPtr. Zero, IntPtr.Zero, file.Directory.FullName, ref cParsed, ref pidlFolder, ref pdw Attrib); if (HR < S_OK) return null; if (!pidlFolder.Equals(IntPtr.Zero)) { HR = folder.BindToObject(pidlFol der, IntPtr.Zero, ref IID_ISHELLFOLDER, ref item); if (HR < S_OK) return null;

if ((item != null)) { HR = item.ParseDisplayNa me(IntPtr.Zero, IntPtr.Zero, file.Name, ref 0, ref pidl, ref 0); Marshal.ThrowExceptionFo rHR(HR); int prgf = 0; HR = item.GetUIObjectOf( 0, 1, new IntPtr[] { pidl }, ref IID_IEXTRACTIMAGE, ref prgf, ref extractImage); if (HR < S_OK) return null; if ((extractImage != nul l)) { StringBuilder lo cation = new StringBuilder(MAX_PATH, MAX_PATH); int priority = 0 ; int requestedCol orDepth = 32; int uFlags = IEI FLAG.IEIFLAG_ASPECT | IEIFLAG.IEIFLAG_ORIGSIZE | IEIFLAG.IEIFLAG_QUALITY; HR = extractImag e.GetLocation(location, location.Capacity, ref priority, ref imageSize, requeste dColorDepth, ref uFlags); if (HR < S_OK) return n ull; HR = extractImag e.Extract(ref hBmp); if (HR < S_OK) return n ull; if (!hBmp.Equals (IntPtr.Zero)) { thumbnai l = Bitmap.FromHbitmap(hBmp); } } } } } } finally { if (!hBmp.Equals(IntPtr.Zero)) DeleteObject(hBmp); if (!pidlFolder.Equals(IntPtr.Zero)) { Marshal.FreeCoTaskMem(pidlFolder); } if ((extractImage != null)) { Marshal.ReleaseComObject(extractImage); extractImage = null; } if ((item != null)) { Marshal.ReleaseComObject(item);

item = null; } if ((folder != null)) { Marshal.ReleaseComObject(folder); folder = null; } } } return thumbnail; } private const int S_OK = 0; public static readonly Guid IID_ContextMenu = new Guid("000214e4-0000-00 00-c000-000000000046"); private const int MAX_PATH = 260; [DllImport("gdi32", CharSet = CharSet.Auto)] private static extern int DeleteObject(IntPtr hObject); [DllImport("shell32", CharSet = CharSet.Auto, SetLastError = true, Exact Spelling = true)] private static extern int SHGetDesktopFolder(ref IShellFolder ppshf); } [ComImportAttribute(), GuidAttribute("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"), In terfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] internal interface IExtractImage { [PreserveSig()] int GetLocation( [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszPathBuffer, int cch, ref int pdwPriority, ref Size prgSize, int dwRecClrDepth, ref int pdwFlags); [PreserveSig()] int Extract( [Out()] ref IntPtr phBmpThumbnail); } [ComImportAttribute(), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnkno wn), Guid("000214E6-0000-0000-C000-000000000046")] internal interface IShellFolder { [PreserveSig()] int ParseDisplayName(IntPtr hwndOwner, IntPtr pbcReserved, [Marshal As(UnmanagedType.LPWStr)] string lpszDisplayName, ref int pchEaten, ref IntPtr ppidl, ref int pdwAttribute s); [PreserveSig()] int EnumObjects(int hwndOwner, [MarshalAs(UnmanagedType.U4)] int grfFlags, ref IntPtr ppenumIDList); [PreserveSig()] int BindToObject(IntPtr pidl, IntPtr pbcReserved, ref Guid riid, ref ISh ellFolder ppvOut); //IShellFolder) As Integer

[PreserveSig()] int BindToStorage(IntPtr pidl, IntPtr pbcReserved, ref Guid riid, IntPtr ppvObj); [PreserveSig()] int CompareIDs(IntPtr lParam, IntPtr pidl1, IntPtr pidl2); [PreserveSig()] int CreateViewObject(IntPtr hwndOwner, ref Guid riid, ref IntPtr ppvOut) ; //IUnknown) As Integer [PreserveSig()] int GetAttributesOf(int cidl, ramindex = 0)] IntPtr[] apidl, ref int rgfInOut); [MarshalAs(UnmanagedType.LPArray, sizepa

[PreserveSig()] int GetUIObjectOf(int hwndOwner, int cidl, [MarshalAs(UnmanagedType .LPArray, sizeparamindex = 0)] IntPtr[] apidl, ref Guid riid, [Out()] ref int prgfInOut, [Out(), MarshalAs(UnmanagedType.IUnknown)] ref object ppvOut); //ByRef ppvOut As IUnknown) As Integer //ByRef ppvOut As IDropTarget) As Integer [PreserveSig()] int GetDisplayNameOf(IntPtr pidl, int uFlags, IntPtr lpName); [MarshalAs(UnmanagedType.U4)]

[PreserveSig()] int SetNameOf(int hwndOwner, IntPtr pidl, .LPWStr)] string lpszName, [MarshalAs(UnmanagedType.U4)] int uFlags, ref IntPtr ppidlOut); }

[MarshalAs(UnmanagedType

[Flags()] internal enum IEIFLAG { IEIFLAG_ASYNC = 0x1, // ask the extractor if it supports ASYNC extract (free threaded) IEIFLAG_CACHE = 0x2, //returned from the extractor if it does NOT cache the thumbnail IEIFLAG_ASPECT = 0x4, // passed to the extractor to beg it to render to the aspect ratio of th e supplied rect IEIFLAG_OFFLINE = 0x8, // if the extractor shouldn't hit the net to get any content needed for the rendering IEIFLAG_GLEAM = 0x10, //does the image have a gleam ? this will be returned if it does IEIFLAG_SCREEN = 0x20, // render as if for the screen (this is exlusive with IEIFLAG_ASPECT ) IEIFLAG_ORIGSIZE = 0x40, // render to the approx size passed, but crop if neccessary IEIFLAG_NOSTAMP = 0x80, // returned from the extractor if it does NOT want an icon stamp on the

thumbnail IEIFLAG_NOBORDER = 0x100, //returned from the extractor if it does NOT want an a border around the thumbnail IEIFLAG_QUALITY = 0x200 // passed to the Extract method to indicate that a slower, higher qualit y image is desired, re-compute the thumbnail }

You might also like