使用WindowsShell获取文件缩略图

HRESULT KThumbnailGetter::GetShellThumbnailImage(LPCWSTR pszPath, HBITMAP* pThumbnail)
{
    HRESULT hr;
    *pThumbnail = NULL;

    LPITEMIDLIST pidlItems = NULL, pidlURL = NULL, pidlWorkDir;
    WCHAR szBasePath[MAX_PATH], szFileName[MAX_PATH];
    WCHAR* p;
    wcscpy(szBasePath, pszPath);
    p = wcsrchr(szBasePath, L'\\');
    if (p) *(p + 1) = L'\0';
    wcscpy(szFileName, pszPath + (p - szBasePath) + 1);

    do
    {
        CComPtr<IShellFolder> psfDesktop;
        hr = SHGetDesktopFolder(&psfDesktop);
        if (FAILED(hr)) 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,获取任意文件缩略图需要使用操作系统提供的 API 函数。以下是 Windows 平台下获取文件缩略图的示例代码: ```c #include <windows.h> #include <shobjidl.h> #include <stdio.h> int main() { HRESULT hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { IShellItemImageFactory* pImageFactory; WCHAR wszFilePath[MAX_PATH] = L"C:\\test.jpg"; // 文件路径 SHCreateItemFromParsingName(wszFilePath, NULL, IID_PPV_ARGS(&pImageFactory)); SIZE size = { 100, 100 }; // 缩略图大小 HBITMAP hBitmap; pImageFactory->GetImage(size, SIIGBF_THUMBNAILONLY, &hBitmap); if (hBitmap) { // 保存缩略图文件 BITMAP bmp; GetObject(hBitmap, sizeof(bmp), &bmp); BITMAPFILEHEADER bfh = { 0 }; bfh.bfType = 0x4d42; // "BM" bfh.bfSize = sizeof(bfh) + sizeof(BITMAPINFOHEADER) + bmp.bmWidthBytes * bmp.bmHeight; bfh.bfOffBits = sizeof(bfh) + sizeof(BITMAPINFOHEADER); BITMAPINFOHEADER bih = { 0 }; bih.biSize = sizeof(bih); bih.biWidth = bmp.bmWidth; bih.biHeight = bmp.bmHeight; bih.biPlanes = 1; bih.biBitCount = 24; FILE* fp; fopen_s(&fp, "thumbnail.bmp", "wb"); fwrite(&bfh, sizeof(bfh), 1, fp); fwrite(&bih, sizeof(bih), 1, fp); BYTE* pBits = new BYTE[bmp.bmWidthBytes * bmp.bmHeight]; GetBitmapBits(hBitmap, bmp.bmWidthBytes * bmp.bmHeight, pBits); fwrite(pBits, bmp.bmWidthBytes * bmp.bmHeight, 1, fp); fclose(fp); delete[] pBits; DeleteObject(hBitmap); } pImageFactory->Release(); CoUninitialize(); } return 0; } ``` 上述代码通过调用 `SHCreateItemFromParsingName` 函数创建一个 `IShellItemImageFactory` 对象,然后调用 `GetImage` 函数获取缩略图,并将缩略图保存到文件中。注意,需要链接 `ole32.lib` 和 `windowscodecs.lib` 库文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值