[try Beta version]
Not logged in

 
 
Creating a thumbnail

Apr 29, 2013 at 3:55pm
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
Apr 29, 2013 at 7:24pm
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;


}
May 21, 2013 at 10:06am
nice sharing ,Im working it right now. but Im totally a newbie in thumbnail creating .and I also found thumbnail generating sdk by this site: http://www.rasteredge.com/how-to/csharp-imaging/thumbnail-creating/
Does this work ?can you give me some advice?
May 21, 2013 at 2:09pm
Cool!

But you need to rework the way you handle errors a bit, as you need to release all interface you have successfully acquired before returning.

For example, if BindToObject fails on line 18, you need to release the IShellFolder interface (pDesktop) and the PIDL (pidl) that you have already acquired.

Actually, I don't see you freeing the PIDL in the success case, either. You need to free it using CoTaskMemFree();

And you need to release the IExtractImage interface (pIExtract), too!

Might it make sense to rework the code so it only has a single point of return?

Andy
Last edited on May 21, 2013 at 2:11pm
Topic archived. No new replies allowed.