Skip to content

Instantly share code, notes, and snippets.

@espresso3389
Created February 27, 2016 19:39
Show Gist options
  • Save espresso3389/e69a22f4b2fdbedd3dff to your computer and use it in GitHub Desktop.
Save espresso3389/e69a22f4b2fdbedd3dff to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
namespace FileThumbnailExtractor
{
public static class FileThumbnailExtractor
{
[StructLayoutAttribute(LayoutKind.Sequential)]
struct SIZE
{
public int cx;
public int cy;
}
[Flags]
enum SIIGBF
{
RESIZETOFIT = 0,
BIGGERSIZEOK = 1,
MEMORYONLY = 2,
ICONONLY = 4,
THUMBNAILONLY = 8,
INCACHEONLY = 0x10,
CROPTOSQUARE = 0x20,
WIDETHUMBNAILS = 0x40,
ICONBACKGROUND = 0x80,
SCALEUP = 0x100,
}
[ComImport, Guid("bcc18b79-ba16-442f-80c4-8a59c30c463b"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IShellItemImageFactory
{
[PreserveSig]
void GetImage(SIZE size, SIIGBF flags, out IntPtr phbm);
}
[DllImport("shell32"), PreserveSig]
extern static void SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string pszPath, IntPtr pbc, [In] ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object iunk);
[DllImport("gdi32")]
extern static int DeleteObject(IntPtr hObject);
public static BitmapSource GetThumbnail(string fileName, int desiredWidth, int desiredHeight, ThumbnailType type)
{
object iunk = null;
IntPtr hBmp = IntPtr.Zero;
try
{
var IID_IShellItemImageFactory = new Guid("bcc18b79-ba16-442f-80c4-8a59c30c463b");
SHCreateItemFromParsingName(fileName, IntPtr.Zero, ref IID_IShellItemImageFactory, out iunk);
var factory = (IShellItemImageFactory)iunk;
SIIGBF flags = SIIGBF.BIGGERSIZEOK;
if (type == ThumbnailType.Icon) flags |= SIIGBF.ICONONLY;
else if (type == ThumbnailType.Thumbnail) flags |= SIIGBF.THUMBNAILONLY;
factory.GetImage(new SIZE { cx = desiredWidth, cy = desiredHeight }, flags, out hBmp);
var bmp = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBmp, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
bmp.Freeze();
return bmp;
}
finally
{
DeleteObject(hBmp);
Marshal.ReleaseComObject(iunk);
}
}
public static async Task<BitmapSource> GetThumbnailAsync(string fileName, int desiredWidth, int desiredHeight, ThumbnailType type)
{
return await Task.Run(() => GetThumbnail(fileName, desiredWidth, desiredHeight, type));
}
}
[Flags]
public enum ThumbnailType
{
Icon = 1,
Thumbnail = 2,
Any = 3,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment