1. 程式人生 > >CImage載入多種圖片的方法

CImage載入多種圖片的方法

此方法經測試是可以載入jpg,gif,bmp等常見格式圖片的。

 // 建立一個imagelist

  CImageList imgList;

  imgList.Create(16, 16,  ILC_COLORDDB|ILC_MASK, 0, 1);
   
  // 建立一個bitmap物件指標
  Bitmap *pBmp = new Bitmap(16, 16, PixelFormat32bppRGB);
  
   CString strImageFile = _T("C:\\test.gif");

   if(_access(strImageFile, 0) != 0)  // not exist
   {

        return;
    }
   
   // 將 strImageFile 繪製到pBmp中的對應位置上
   Graphics gc(pBmp);
   gc.SetInterpolationMode(InterpolationModeHighQualityBilinear);
   
   // 從strImageFile生成一個Bitmap
   Gdiplus::Bitmap *pImage = Bitmap::FromFile((const wchar_t *)_bstr_t(strImageFile));

  // 將該Bitmap繪製到pBmp中的正確位置上
  Rect rcTar(16, 0, 16,16);
   

 //先將區域內填充為白色
   Color col(255,255,255);
   SolidBrush sb(col);
   gc.FillRectangle(&sb, rcTar);

// 將影象填充到指定區域

   gc.DrawImage(pImage, rcTar, 
    0, 0, pImage->GetWidth(), pImage->GetHeight(), 
    UnitPixel, 0, 0, 0);   
      
   delete pImage;
  }
  
  // 從pBmp獲得一個HBITMAP, 然後得到CBitmap物件
  HBITMAP hBitmap;
  pBmp->GetHBITMAP(1, &hBitmap); 

  CBitmap bmp;
  bmp.Attach(hBitmap);   
  
  imgList.Add(&bmp, (255, 255, 255));
  delete pBmp;

  
  m_List.SetImageList(&imgList, LVSIL_SMALL);