• 0

[C#] Getting a file's large icon


Question

Hello,

I'm trying to get file icons using SHGetFileInfo (See attched image), but when I try to get the large icon of the file using SHGFI_LARGEICON, it returnes the 32x32 icon :( while I want to get the 48x48 icon.

I've figured out that if I set Windows to show large icons, I can use SHGFI_SHELLICONSIZE, but it doesn't work when Windows is set to show small icons :blink:

How can I always get the large 48x48 icon of a file? What do I need to use?

Thanks in advance for your help :)

post-62656-1115302870_thumb.jpg

Link to comment
https://www.neowin.net/forum/topic/316840-c-getting-a-files-large-icon/
Share on other sites

14 answers to this question

Recommended Posts

  • 0

Thanks for your answer :) However, I have no idea how to use SHDefExtractIcon with C#. Maybe you can tell me how?

I just want to get the large icon of any real file on the computer. I just want it to work like the Windows toolbar - it shows the icons of the files in a certain directory.

Any idea how I can use SHDefExtractIcon ?

  • 0

Setting the icons sizes just for this seems quite weird to me :blink: I don't think it's the way to do it but thanks for the creative idea :)

I have the same problem with IExtractIcon::Extract - I have no idea how to use it :( Can anybody show me an example? How do I use it in an application?

BTW, thanks for your suggestions :)

  • 0

Here's how I do it:

HRESULT CPidl::GetParentShellFolder(IShellFolder** folder,LPCITEMIDLIST*

relativePidl, IShellFolder2** folder2)const
{
	ATLASSERT(m_Pidl != NULL);
	ATLASSERT(folder != NULL);

	HRESULT _hRes;

	if(folder == NULL)
	{
 ?_hRes = E_INVALIDARG;
	}
	else if(NULL == m_Pidl)
	{
 ?_hRes = E_FAIL;
	}
	else
	{
 ?*folder = NULL;

 ?LPCITEMIDLIST pidlChild = {0};

 ?HRESULT _hRes = ?SHBindToParent(m_Pidl,
 ? ? ? ? ? ? ? ? ? ? ? ? ? ?IID_IShellFolder, 
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(void**) folder, 
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?const_cast<LPCITEMIDLIST*>(&pidlChild));

 ?if(relativePidl != NULL)
 ?{
 ?	*relativePidl = pidlChild;
 ?}

 ?if(folder2 != NULL)
 ?{
 ?	*folder2 = NULL;

 ?	if(SUCCEEDED(_hRes) && *folder != NULL)
 ? ?(*folder)->QueryInterface(IID_IShellFolder2,reinterpret_cast<void**>(folder2));
 ?}
	}

	return _hRes;
}


HICON CPidl::ExtractIcon(UINT size, UINT flags) const
{
	ATLASSERT(m_Pidl != NULL);

	if(NULL == m_Pidl)
 ?return NULL;

	CComPtr<IShellFolder> parentFolder;
	LPCITEMIDLIST relativePidl = NULL;

	HRESULT _hRes = GetParentShellFolder(&parentFolder,&relativePidl);

	if( FAILED(_hRes) )
 ?return NULL;

	CComPtr<IExtractIcon> extractIcon;
	_hRes = parentFolder->GetUIObjectOf(NULL,1,
const_cast<LPCITEMIDLIST*>(&relativePidl),
IID_IExtractIcon,NULL,reinterpret_cast<void**>(&extractIcon) );

	if( FAILED(_hRes) || NULL == extractIcon.p)
 ?return NULL;

	TCHAR location[MAX_PATH+2] = {0};
 // +2 fixes bogus shellextension off-by-one errors.
	int ? index(0);
	UINT outFlags(GIL_FORSHELL);

	_hRes = extractIcon->GetIconLocation(GIL_FORSHELL,
location,MAX_PATH,
&index,&outFlags);

	if( FAILED(_hRes) )
 ?return NULL;

	HICON smallIcon = {0};
	HICON largeIcon = {0};

	size = MAKELONG (size,size);

	_hRes = extractIcon->Extract(location,index,&largeIcon,&smallIcon,size);

	if( FAILED(_hRes) )
 ?return NULL;

	if(largeIcon != NULL)
	{
 ?DestroyIcon(smallIcon);
 ?return largeIcon;
	}
	else
	{
 ?return smallIcon;
	}

}

  • 0

1) Get a PIDL to the file you want the icon of.

2) Get the IShellFolder parent of it and the relative PIDL.

3) Query that IShellFolder for an IExtractIcon (GetUIObjectOf) for the relative PIDL you have obtained in step2.

4) get the location of the object with IExtractIcon::GetIconLocation

5) Use that location to extract the icon with IExtractIcon::Extract(...) and give up the size as a long with the high word and low word of the long set as the size ( MAKELONG(size, size) ).

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • The sound never left. My Windows 10 installation still has it enabled, I found the checkbox in an earlier build and turned it on and my PC chimes every time it cold boots. That's how I know Windows Update didn't shut down after updating despite me telling it to. That being said... seeing Apple Liquid Glass has me thinking, if Windows 11 brought back the 7 Aero Glass running on top of the new kernel, minus the AI crap, I might actually be willing to move to it.
    • These days the differences are small, so if you're a petty nerd over dumb things, you'll be forever stuck behind and won't ever enjoy the latest and current niceties. Yup, it's a real life lesson too.
    • Microsoft 365 Roadmap Weekly: New features for the new Outlook, Copilot updates and more by David Uzondu This week, Microsoft added a slew of new entries to the public Microsoft 365 Roadmap, including plans for Teams, the new Outlook, and, of course, Copilot and Copilot Chat. Starting with Outlook, three practical updates are headed for the new Outlook for Windows client. First up, you'll finally be able to add attachments to emails while you are offline, a basic function that has been sorely missed. The remaining two features are slated for August. One will let you create a rule that specifically triggers a desktop alert. The other adds a folder deletion warning. If you try to delete a folder on which an email rule depends, Outlook will stop you and prompt you to fix the rule first. Like Google with Gemini, Microsoft continues to push Copilot deeper into its applications, with several updates planned for mid-2025. Later this month, Copilot will be able to give you a quick summary of recent changes and comments on a file right from the backstage view in Word, Excel, and PowerPoint, before you even open the document. Also, Copilot Chat will get the ability to limit its responses to selected content sources, like specific SharePoint sites or files, which should make its answers more focused. Next month, PowerPoint users will get two more Copilot tricks. You will be able to rewrite selected text using a custom natural language prompt and add entirely new topics to an existing presentation, with Copilot generating slides that match the existing look and feel. Edge is not left out, as its integrated Copilot Chat will soon be able to access content across multiple open tabs, not just the active one, with that update scheduled for August. Finally, for organizations that heavily use Teams Rooms Pro on Windows, an August update will allow for simultaneous town hall and webinar views. This will keep the presenter's backstage controls, like green room and backroom chat, hidden from the main audience display in the room.
    • The only reason I have Windows 11 is because it came with the new machine. I was quite happy with Windows 10. Windows 11 has its own issues just like any OS, but honestly after the fear-mongering towards Windows 11 while I was on Windows 10, I thought I would be more frustrated. I still prefer my daily OS, but Windows 11 isn't the worst thing in the world for what I use it for.
  • Recent Achievements

    • Apprentice
      Wireless wookie went up a rank
      Apprentice
    • Week One Done
      bukro earned a badge
      Week One Done
    • One Year In
      Wulle earned a badge
      One Year In
    • One Month Later
      Wulle earned a badge
      One Month Later
    • One Month Later
      Simmo3D earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      610
    2. 2
      ATLien_0
      285
    3. 3
      +FloatingFatMan
      179
    4. 4
      Michael Scrip
      151
    5. 5
      Steven P.
      112
  • Tell a friend

    Love Neowin? Tell a friend!