Creating a thumbnail

hello everyone
I am trying to get thumbnail from a file,the same icons you see in windows explorer. this file can be of any type.
however, I don't know how to do this.
I am not using MFC or anything, just the WinAPI
After some hard work, I manged to do it myself. I refer you to http://msdn.microsoft.com/en-us/library/aa289172%28VS.71%29.aspx for explaination. here is the code that gets the job done:
you need to include Shobjidl.h and Shlobj.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
HBITMAP GetThumbnail(wstring File)
{
	wstring Folder,FileName;
	int Pos = File.find_last_of(L"\\");
	Folder = File.substr(0,Pos);
	FileName = File.substr(Pos+1);

	IShellFolder* pDesktop = NULL;
	IShellFolder* pSub = NULL;
	IExtractImage* pIExtract = NULL;
	LPITEMIDLIST pidl = NULL;

	HRESULT hr;
	hr = SHGetDesktopFolder(&pDesktop);
	if(FAILED(hr)) return NULL;
	hr = pDesktop->ParseDisplayName(NULL, NULL, (LPWSTR)Folder.c_str(), NULL, &pidl, NULL);
	if(FAILED(hr)) return NULL;
	hr = pDesktop->BindToObject(pidl, NULL, IID_IShellFolder, (void**)&pSub);
	if(FAILED(hr)) return NULL;
	hr = pSub->ParseDisplayName(NULL, NULL, (LPWSTR)FileName.c_str(), NULL, &pidl, NULL);
	if(FAILED(hr)) return NULL;
	hr = pSub ->GetUIObjectOf(NULL, 1, (LPCITEMIDLIST *)&pidl, IID_IExtractImage, NULL, (void**)& pIExtract);
	if(FAILED(hr)) return NULL;

	SIZE size;
	size.cx =300;
	size.cy =300;

	DWORD dwFlags = IEIFLAG_ORIGSIZE | IEIFLAG_QUALITY;

	HBITMAP hThumbnail = NULL;

	// Set up the options for the image
	OLECHAR pathBuffer[MAX_PATH];
	hr = pIExtract->GetLocation(pathBuffer, MAX_PATH, NULL, &size,32, &dwFlags);

	// Get the image
	hr = pIExtract ->Extract(&hThumbnail);

	pDesktop->Release();
	pSub->Release(); 

	return hThumbnail;


}
Registered users can post here. Sign in or register to post.