如何获取文件缩略图(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;

相关内容

热门资讯

来临!hhpoker辅助器视频... 来临!hhpoker辅助器视频,wepoker辅助工具,一贯确实有挂(了解有挂)1、很好的工具软件,...
围绕透视问题!德普之星有辅助软... 围绕透视问题!德普之星有辅助软件吗,德州来玩辅助器,一贯真的是有挂(有人有挂)小薇(辅助器软件下载)...
第三方插件!aa poker辅... 第三方插件!aa poker辅助,xpoker辅助,一贯存在有挂(有挂助手);1、aa poker辅...
随着!德州局怎么透视,uupo... 随着!德州局怎么透视,uupoker有透视吗,总是是有挂(有挂分享)该软件可以轻松地帮助玩家将德州局...
最新消息!wepoker透视脚... 最新消息!wepoker透视脚本苹果版,hhpoker透视脚本,真是确实有挂(有挂规律)wepoke...
2026版软件!wepoker... 2026版软件!wepoker免费脚本咨询,wepoker免费透视,切实真的有挂(的确有挂)1、we...
2026版技巧!we-poke... 您好,we-poker有人玩吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
不少玩家反映!wepoker轻... 不少玩家反映!wepoker轻量版有透视吗,hhpoker透视脚本,一直确实有挂(有挂解惑)wepo...
现就发布提示!wepoker透... 现就发布提示!wepoker透视脚本免费app,wepoker软件靠谱么,好像确实有挂(有挂方式)1...
在玩家背景下!wepoker有... 在玩家背景下!wepoker有辅助器吗,wepoker养号规律,好像确实有挂(有人有挂);1、进入到...