本文介绍了提取文件的高分辨率图标或缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows中获取高分辨率图标或缩略图,并提供该文件的完整路径。 不需要是缩略图-外观精美的图标会很棒。我不在乎它是 HICON 还是 HBITMAP :我将其放在GDI +对象中并将其渲染到设备上下文中。

I'm trying to get a high resolution icon or thumbnail in Windows given a full path to that file. Doesn't need to be a thumbnail — a good looking Icon will be great. I Don't care if it's an HICON or an HBITMAP: I'm going to put it in a GDI+ object and render it into a device context.

我尝试使用 SHGetFileInfo (在标志上有各种变化),但返回的像素大小永远不会超过〜32x32,可缩放至128像素左右,以至于我需要。

I've tried using SHGetFileInfo (with various variations on flags), but never get more than a ~32x32 icon back, which scales horribly to the 128 pixels or so that I need.

if (!SHGetFileInfoW(path, 0, &fi, sizeof(fi),
                    SHGFI_ICON | SHGFI_ICONLARGE | SHGFI_TYPENAME))
    return GetLastError();

// fi.hIcon is a valid icon here, but it's horrible quality with
// a black mask on it. I want higher quality, and dare I dream of
// alpha channel? Mask is acceptable, i suppose.

SHGetFileInfo 返回当我用 SHGFI_ICONLOCATION 调用时(这似乎是该API的已知问题)。

SHGetFileInfo returns "" when I call with SHGFI_ICONLOCATION (which appears to be a known problem with that API).

我还尝试使用 SHCreateItemFromParsingName 名称,目的是获取 IThumbnailProvider ,但 BindToHandler 始终为 BHID_ThumbnailHandler ...

I've also tried using SHCreateItemFromParsingName name with the intention of getting IThumbnailProvider, but BindToHandler always returns E_NOTIMPL for BHID_ThumbnailHandler ...

IShellItem *psi;
hr = SHCreateItemFromParsingName(path, NULL, IID_PPV_ARGS(&psi));
if (SUCCEEDED(hr))
{
    IThumbnailProvider *pThumbProvider;
    hr = psi->BindToHandler(NULL, BHID_ThumbnailHandler,
                            IID_PPV_ARGS(&pThumbProvider));
    if (SUCCEEDED(hr))
    { 
    // never get here because hr == E_NOTIMPL !!!

我实际上已经在运行Microsoft ,发现它遇到了与 BindToInterface 相同的问题。

I've actually run the Microsoft Thumbnail Providers Sample and found that it suffers from the same problem with BindToInterface.

那么,关于我还能尝试什么的任何建议?我只想要某种图片-y或多或少地以至少100px的大小表示该文件-比32x32还要好...

So, any suggestions on what else I could try? I just want something picture-y that more or less represents this file at, say, at least 100px size — just anything better than 32x32 ...

推荐答案

如果您的目标是Vista和更高版本,则可以从巨型图像列表中获得256x256的图标。如果您需要使用XP,则至少可以使用48x48的超大图像列表(比32x32的大图像略好)。

If you are targeting Vista and higher, you can get a 256x256 icon from the jumbo image list. If you need to target XP, you can at least use the extra large image list, which is 48x48 (slightly better than 32x32 large).

#include <commoncontrols.h>
#include <shellapi.h>

HICON GetHighResolutionIcon(LPTSTR pszPath)
{
    // Get the image list index of the icon
    SHFILEINFO sfi;
    if (!SHGetFileInfo(pszPath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX)) return NULL;

    // Get the jumbo image list
    IImageList *piml;
    if (FAILED(SHGetImageList(SHIL_JUMBO, IID_PPV_ARGS(&piml))) return NULL;

    // Extract an icon
    HICON hico;
    piml->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hico);

    // Clean up
    piml->Release();

    // Return the icon
    return hico;
}

这篇关于提取文件的高分辨率图标或缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 02:29