This is not a news to get a icon from a file. But how about the process? I can extrace the process image file name and use it to extract the icons that it contains. The output is a handle of HICON. I survey many articles to find out the solution. Finally, I found a solution to the saving issue. The following sample codes will show the GDI+ and save the HICON into a PNG file. Of course, you can save it into other format that GDI+ provided.
HICON GetFileIcon(const CString& strFileName, CString& strSaveFileName, bool bSmallIcon) {
SHFILEINFO sfi;
SHGetFileInfo((LPCTSTR)strFileName,
FILE_ATTRIBUTE_NORMAL,
&sfi,
sizeof(SHFILEINFO),
SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | (bSmallIcon ? SHGFI_SMALLICON : SHGFI_LARGEICON));
if(sfi.hIcon) {
Bitmap* pBitmap = Bitmap::FromHICON(sfi.hIcon);
CLSID encoderClsid;
GetEncoderClsid(L"image/png", &encoderClsid);
CStringW strPngFileName;
#ifndef _UNICODE
strPngFileName.Format(L"%S", strFileName.GetString());
#else
strPngFileName = strFileName;
#endif
int nStart = strPngFileName.ReverseFind('\\') + 1;
int nEnd = strPngFileName.ReverseFind('.');
int nLength = nEnd - nStart;
strPngFileName = strPngFileName.Mid(nStart, nLength);
strPngFileName = CStringW(L"C:\\") + strPngFileName + (bSmallIcon ? CStringW("Small.png") : CStringW("Large.png"));
strSaveFileName = strPngFileName;
pBitmap->Save(strPngFileName.GetString(), &encoderClsid, NULL);
}
return sfi.hIcon;
}
You should include the header file <gdiplus.h> and use the name space "GdiPlus".
沒有留言:
張貼留言