windows获取缩略图

441 阅读1分钟

这是通过windows api获取缩略图的方法,参数是qt的

#include <thumbcache.h>

QImage GetThumbnailEx(const QString& path)
{
	IShellItem* item = nullptr;
        // 需要替换成windows斜杠,否则item获取失败
	QString str = path;
	str = str.replace("/", "\\");
	SHCreateItemFromParsingName(str.toStdWString().c_str(), nullptr, IID_PPV_ARGS(&item));
	if (!item) {
		return nullptr;
	}
	IThumbnailCache* cache = nullptr;
	CoCreateInstance(
		CLSID_LocalThumbnailCache,
		nullptr,
		CLSCTX_INPROC,
		IID_PPV_ARGS(&cache));
	if (!cache) {
		return nullptr;
	}
	ISharedBitmap* shared_bitmap;
	cache->GetThumbnail(
		item,
		48 * 64,
		WTS_FLAGS::WTS_SCALETOREQUESTEDSIZE | WTS_FLAGS::WTS_SCALEUP,
		&shared_bitmap,
		nullptr,
		nullptr);
	if (!shared_bitmap) {
		return nullptr;
	}
	HBITMAP hbitmap = nullptr;
	shared_bitmap->GetSharedBitmap(&hbitmap);
        // 这里需要处理一下bitmap,转换一下传出去
        const auto image = QtWin::fromHBITMAP(bitmap).toImage();
        shared_bitmap->Release();
	cache->Release();
	item->Release();
	return image;
}