如何获取文件缩略图(C#和C++实现)
创始人
2024-11-15 12:09:18
0

在C++中,可以有以下两种办法

使用COM接口IThumbnailCache

文档链接:IThumbnailCache (thumbcache.h) - Win32 apps | Microsoft Learn

示例代码如下:

VOID GetFileThumbnail(PCWSTR path) {     HRESULT hr = CoInitialize(nullptr);      IShellItem* item = nullptr;     hr = SHCreateItemFromParsingName(path, nullptr, IID_PPV_ARGS(&item));      IThumbnailCache* cache = nullptr;     hr = CoCreateInstance(         CLSID_LocalThumbnailCache,         nullptr,         CLSCTX_INPROC,         IID_PPV_ARGS(&cache));      WTS_CACHEFLAGS flags = WTS_LOWQUALITY;     ISharedBitmap* shared_bitmap;     hr = cache->GetThumbnail(         item,         12*16,         WTS_EXTRACT,         &shared_bitmap,         nullptr,         nullptr);      HBITMAP hbitmap = NULL;     hr = shared_bitmap->GetSharedBitmap(&hbitmap);     shared_bitmap->Release();     cache->Release();     CoUninitialize();      //使用hbitmap,使用完成后调用DeleteObject     DeleteObject(hbitmap);  }

前面我一直使用的这种方式,但是最近系统升级后,会报 Generic Error in GDI+ 

后面我升级成了下面这种方式

使用IShellItemImageFactory接口

文档链接:IShellItemImageFactory (shobjidl_core.h) - Win32 apps | Microsoft Learn

官方示例代码:Windows-classic-samples/Samples/Win7Samples/winui/shell/appplatform/UsingImageFactory/ImageFactorySample.cpp at main · microsoft/Windows-classic-samples · GitHub

HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);     HBITMAP hbmp = NULL;      if (SUCCEEDED(hr))     {         IShellItemImageFactory* pImageFactory;         hr = SHCreateItemFromParsingName(path, NULL, IID_PPV_ARGS(&pImageFactory));         if (SUCCEEDED(hr))         {             SIZE size = { 256, 256 };  //图像大小             hr = pImageFactory->GetImage(size, SIIGBF_THUMBNAILONLY, &hbmp);             pImageFactory->Release();         }         CoUninitialize();          //使用hbmp,使用完成后调用DeleteObject         DeleteObject(hbmp);     }

在C#中使用,可以直接nuget引用  WindowsAPICodePack-Shell  包

PM> NuGet\Install-Package WindowsAPICodePack-Shell -Version 1.1.1

然后用下面的代码就可以获取文件缩略图

ShellFile shellFile = ShellFile.FromFilePath(filepath); Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;

相关内容

热门资讯

WPK透视辅助!德州局脚本,w... WPK透视辅助!德州局脚本,wpk德州局透视(切实是有挂)1、超多福利:超高返利,海量正版游戏,wp...
微扑克透视辅助!pokemmo... 微扑克透视辅助!pokemmo辅助器手机版下载,hhpoker开挂教程(一贯是真的有挂)1、微扑克透...
WPK透视辅助!扑克之星辅助,... WPK透视辅助!扑克之星辅助,wepoker辅助是真的假的(真是真的有挂)wepoker辅助是真的假...
wpk透视辅助!hhpoker... wpk透视辅助!hhpoker是内部控制吗,wepoker破解工具(好像真的有挂)1、hhpoker...
微扑克透视辅助!hhpoker... 微扑克透视辅助!hhpoker德州透视,xpoker透视辅助(好像真的是有挂)1)hhpoker德州...
微扑克透视辅助!黑侠破解wep... 微扑克透视辅助!黑侠破解wepoker,wepoker辅助分析器(切实是有挂)1、首先打开wepok...
wpk透视辅助!aapoker... wpk透视辅助!aapoker怎么设置抽水,大菠萝手游辅助(总是真的有挂)运aapoker怎么设置抽...
wpk透视辅助!wepoker... wpk透视辅助!wepoker软件辅助程序,wepoker辅助器安装包定制(其实是有挂)小薇(辅助器...
微扑克透视辅助!aapoker... 微扑克透视辅助!aapoker怎么提高中牌率,hhpoker软件安装包(确实真的有挂)1、进入游戏-...
WPK透视辅助!wepoker... 您好,wepoker透视器免费这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...