?
- //-------------------在内存中建立区域以存放所得位图------------------- // hBitmapSrc 为 CBitmap中保存的矩形原图资源句柄
- // hDC 句柄 // 在内存中开辟位图资源,用以保存原图
- HBITMAP CopyHBitmap(HBITMAP hBitmapSrc,HDC hDC) {
- BITMAP bm;
- HBITMAP hBitmapDst; HDC hdcSrc,hdcDst;
- GetObject(hBitmapSrc,sizeof(BITMAP),&bm);
- hBitmapDst=CreateCompatibleBitmap(hDC,bm.bmWidth,bm.bmHeight);
- hdcSrc=CreateCompatibleDC(hDC); hdcDst=CreateCompatibleDC(hDC);
- SelectObject(hdcSrc,hBitmapSrc);
- SelectObject(hdcDst,hBitmapDst);
- BitBlt(hdcDst,0,0,bm.bmWidth,bm.bmHeight,hdcSrc,0,0,SRCCOPY);
- DeleteDC(hdcSrc); DeleteDC(hdcDst);
- return hBitmapDst;
- }
下面给大家一个具体实例:将CBitmap类中的图像保存到文件
?
- // 使用下面的代码,可以把CBitmap类中的图像保存到图像文件中。支持格式:BMP、JPG、GIF和PNG。
- void SaveBitmap(CString strFilePath, CBitmap Bitmap) {
- if ( Bitmap.m_hObject ) {
- CImage imgTemp; // CImage是MFC中的类。 imgTemp.Attach(Bitmap.operator HBITMAP());
- imgTemp.Save(strFilePath); }
- }
- // 注意文件路径名strFilePath必须包含后缀,即BMP、JPG、GIF或PNG中的一种。
最后附上CBitmap,HBitmap,Bitmap区别及联系
加载一位图,可以使用LoadImage:
HANDLE LoadImage(HINSTANCE hinst,LPCTSTR lpszName,UINT uType,int cxDesired,int CyDesired,UINT fuLoad);










