Jump to content

Get a file's thumbnail or icon (Win32)


Josh
 Share

Recommended Posts

There are too many unknown factors to make this code part of the official API yet, so I am posting it here for now:

#include "UltraEngine.h"
#include "GetFileIcon.h"
#ifdef _WIN32
#include <commoncontrols.h>
#endif

namespace UltraEngine::Editor
{
	shared_ptr<Pixmap> GetFileIcon(const WString& file, const int size)
	{
		shared_ptr<Pixmap> pixmap;
#ifdef _WIN32
		CoInitialize(nullptr);
		WString path = RealPath(file).Replace("/", "\\");

		IShellItemImageFactory* pImageFactory;
		IShellItem* psiFile;
		LPCWSTR szFilePath = path.Data();
		auto hr = SHCreateItemFromParsingName(szFilePath, NULL, IID_PPV_ARGS(&pImageFactory));
		if (SUCCEEDED(hr))
		{
			HBITMAP hbmp;
			SIZE sz = { size,size };
			//SIIGBF_BIGGERSIZEOK
			//SIIGBF_RESIZETOFIT //this may invert some images
			hr = pImageFactory->GetImage(sz, SIIGBF_RESIZETOFIT | SIIGBF_BIGGERSIZEOK, &hbmp);
			if (SUCCEEDED(hr))
			{
				BITMAP bminfo = {};
				if (GetObjectW(hbmp, sizeof(bminfo), &bminfo) != 0)
				{
					auto hdc = GetDC(GetDesktopWindow());
					BITMAPINFO bmi = {};
					bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);					
					auto r = GetDIBits(hdc, hbmp, 0, 1, nullptr, &bmi, DIB_RGB_COLORS);
					TextureFormat format = TextureFormat(0);
					if (bminfo.bmBitsPixel == 24) format = TEXTURE_BGR;
					if (bminfo.bmBitsPixel == 32) format = TEXTURE_BGRA;
					bmi.bmiHeader.biHeight = -Abs(bmi.bmiHeader.biHeight);
					bmi.bmiHeader.biCompression = BI_RGB;
					if (format != 0)
					{
						pixmap = CreatePixmap(bminfo.bmWidth, bminfo.bmHeight, format);
						Assert(pixmap->pixels->GetSize() == bmi.bmiHeader.biSizeImage);
						auto r = GetDIBits(hdc, hbmp, 0, bminfo.bmHeight, pixmap->pixels->Data(), &bmi, DIB_RGB_COLORS);
					}
				}
				DeleteObject(hbmp);
			}
			pImageFactory->Release();
		}
#endif
		return pixmap;
	}
}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...