Skip to content

IExtractImage

  1. void* CShellListCtrl::GetThumbnailImage(CString &fileName, int longestEdge, int colorDepth)
  2. {
  3.  // divide the file name into a folder path and file name.
  4.  WCHAR wDirName[MAX_PATH];
  5.  WCHAR wFileName[MAX_PATH];
  6.  CString dir = fileName.Left(fileName.ReverseFind('\\'));
  7.  CString file = fileName.Right(fileName.GetLength() - dir.GetLength() - 1);
  8.  TCHAR tName[MAX_PATH];
  9.  strcpy(tName, dir.GetBuffer());
  10.  wcscpy(wDirName, CT2W(tName));
  11.  
  12.  strcpy(tName, file.GetBuffer());
  13.  wcscpy(wFileName, CT2W(tName));
  14.  IShellFolder* pDesktop = NULL;
  15.  IShellFolder* pSub = NULL;
  16.  IExtractImage* pIExtract = NULL;
  17.  LPITEMIDLIST pList = NULL;
  18.  HBITMAP   hBmp = NULL;
  19.  
  20.  // get the desktop directory
  21.  if (SUCCEEDED(SHGetDesktopFolder(&pDesktop)))
  22.  {
  23.   // get the pidl for the directory
  24.   HRESULT hr = pDesktop->ParseDisplayName(NULL, NULL, wDirName, NULL, &pList, NULL);
  25.   if (FAILED(hr))
  26.   { 
  27.    throw new CUserInterfaceException("Failed to parse the directory name");
  28.   }
  29.  
  30.   // get the directory IShellFolder interface
  31.   hr = pDesktop->BindToObject(pList, NULL, IID_IShellFolder, (void**)&pSub);
  32.   if (FAILED(hr))
  33.   {
  34.    throw new CUserInterfaceException("Failed to bind to the directory");
  35.   }
  36.  
  37.   // get the file's pidl
  38.   hr = pSub->ParseDisplayName(NULL, NULL, wFileName, NULL, &pList, NULL);
  39.   if (FAILED(hr))
  40.   {
  41.    throw new CUserInterfaceException("Failed to parse the file name");
  42.   }
  43.  
  44.   // get the IExtractImage interface
  45.   LPCITEMIDLIST pidl = pList;
  46.   hr = pSub->GetUIObjectOf(NULL, 1, &pidl, IID_IExtractImage, NULL, (void**)&pIExtract);
  47.   
  48.   // set our desired image size
  49.   SIZE size;
  50.   size.cx = longestEdge;
  51.   size.cy = longestEdge;     
  52.   
  53.   if(pIExtract == NULL)
  54.   {
  55.    return NULL;
  56.   }        
  57.  
  58.   // The IEIFLAG_ORIGSIZE flag tells it to use the original aspect
  59.   // ratio for the image size. The IEIFLAG_QUALITY flag tells the
  60.   // interface we want the image to be the best possible quality.
  61.   DWORD dwFlags = IEIFLAG_ORIGSIZE | IEIFLAG_QUALITY;     
  62.   
  63.   OLECHAR pathBuffer[MAX_PATH];
  64.   hr = pIExtract->GetLocation(pathBuffer, MAX_PATH, NULL, &size, colorDepth, &dwFlags);        
  65.   if (FAILED(hr))
  66.   {
  67.    throw new CUserInterfaceException("The call to GetLocation failed");
  68.   }
  69.  
  70.   hr = pIExtract->Extract(&hBmp)//THIS IS WHERE I GET THE ERROR MESSAGE
  71.  
  72.   pIExtract->Release();
  73.  } // if
  74.  
  75.  // Release the COM objects we have a reference to.
  76.  pDesktop->Release();
  77.  pSub->Release();
  78.  
  79.  return hBmp;
  80. }

Post a Comment

Your email is never published nor shared. Required fields are marked *