GDI 总结一 CBitmap类

CBitmap类:

 

class CBitmap : public CGdiObject
{
	DECLARE_DYNAMIC(CBitmap)

public:
	static CBitmap* PASCAL FromHandle(HBITMAP hBitmap);

// Constructors
	CBitmap();

	BOOL LoadBitmap(LPCTSTR lpszResourceName);
	BOOL LoadBitmap(UINT nIDResource);
	BOOL LoadOEMBitmap(UINT nIDBitmap); // for OBM_/OCR_/OIC_
#ifndef _AFX_NO_AFXCMN_SUPPORT
	BOOL LoadMappedBitmap(UINT nIDBitmap, UINT nFlags = 0,
		LPCOLORMAP lpColorMap = NULL, int nMapSize = 0);
#endif
	BOOL CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitcount,
			const void* lpBits);
	BOOL CreateBitmapIndirect(LPBITMAP lpBitmap);
	BOOL CreateCompatibleBitmap(CDC* pDC, int nWidth, int nHeight);
	BOOL CreateDiscardableBitmap(CDC* pDC, int nWidth, int nHeight);

// Attributes
	operator HBITMAP() const;
	int GetBitmap(BITMAP* pBitMap);

// Operations
	DWORD SetBitmapBits(DWORD dwCount, const void* lpBits);
	DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits) const;
	CSize SetBitmapDimension(int nWidth, int nHeight);
	CSize GetBitmapDimension() const;

// Implementation
public:
	virtual ~CBitmap();
#ifdef _DEBUG
	virtual void Dump(CDumpContext& dc) const;
#endif
};


 

class CGdiObject : public CObject
{
	DECLARE_DYNCREATE(CGdiObject)
public:

// Attributes
	HGDIOBJ m_hObject;                  // must be first data member
	operator HGDIOBJ() const;
	HGDIOBJ GetSafeHandle() const;

	static CGdiObject* PASCAL FromHandle(HGDIOBJ hObject);
	static void PASCAL DeleteTempMap();
	BOOL Attach(HGDIOBJ hObject);
	HGDIOBJ Detach();

// Constructors
	CGdiObject(); // must Create a derived class object
	BOOL DeleteObject();

// Operations
#pragma push_macro("GetObject")
#undef GetObject
	int _AFX_FUNCNAME(GetObject)(int nCount, LPVOID lpObject) const;
	int GetObject(int nCount, LPVOID lpObject) const;
#pragma pop_macro("GetObject")
	UINT GetObjectType() const;
	BOOL CreateStockObject(int nIndex);
	BOOL UnrealizeObject();
	BOOL operator==(const CGdiObject& obj) const;
	BOOL operator!=(const CGdiObject& obj) const;

// Implementation
public:
	virtual ~CGdiObject();
#ifdef _DEBUG
	virtual void Dump(CDumpContext& dc) const;
	virtual void AssertValid() const;
#endif
};

 

 


 

1 装载已导入工程的位图资源

 

	// 装载位图

	CBitmap bmp;
	bmp.LoadBitmap(IDB_BITMAP);


 

2 装载位图文件

    为了能让CBitmap能够装载位图文件,必须调用API函数LoadImage

 

HANDLE LoadImage(
  HINSTANCE hinst,   // handle of the instance containing the image
  LPCTSTR lpszName,  // name or identifier of image
  UINT uType,        // type of image
  int cxDesired,     // desired width
  int cyDesired,     // desired height
  UINT fuLoad        // load flags
);

 

装载:

 Example 1:

	HBITMAP hBmp = (HBITMAP)LoadImage(NULL,
		m_fileName,
		IMAGE_BITMAP, 
		0, 0, 
		LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);

 

Example 2:

    HBITMAP	hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
		"BG.bmp",
		IMAGE_BITMAP,
		0,0,
		LR_LOADFROMFILE);


将装载后得到的HBITMAP资源句柄 与 CBitmap 对象 相连

 

	if (hBmp != NULL) {
		CBitmap *pBmp = CBitmap::FromHandle(hBmp);
	}


     OR

	CBitmap bmp;
	if (hBmp != NULL) {
		bmp.DeleteObject();
		bmp.Attach(hBmp);	
	}


 

3 显示位图

 

 

	CBitmap bmp;
	bmp.LoadBitmap(IDB_BITMAP1);
	
	BITMAP bm;
	bmp.GetBitmap(&bm);

	CDC dc;
	dc.CreateCompatibleDC(pDC);
	CBitmap*pOldBmp=(CBitmap *)dc.SelectObject(&bmp);

	pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dc,0,0,SRCCOPY);
	pDC->SelectObject(pOldBmp);

	bmp.DeleteObject();
	bmp.LoadBitmap(IDB_BITMAP2);


4 删除资源

 

错误方式:

	CBitmap bmp;
	bmp.LoadBitmap(IDB_BITMAP);

	CBitmap *pOld=pDC->SelectObject(&bmp);

	// 此时位图对象还在pDC中,因此不能删除  下面的语句是错误的
	bmp.DeleteObject();


 

正确方式:

	CBitmap bmp;
	bmp.LoadBitmap(IDB_BITMAP);

	CBitmap *pOld=pDC->SelectObject(&bmp);

	// 此时位图对象还在pDC中,因此不能马上删除
	// 而是先将位图从DC中选出 然后再删除
	pDC->SelectObject(pOld);
	bmp.DeleteObject();


 

5 CBitmap 析构

 

当CBitmap作为局部变量 在其退出作用范围后,会发生析构,这时候CBitmap会将其对应的位图资源(hBitmap )释放掉。

若想继续使用该位图资源hBitmap,则在退出作用范围前,应将位图资源hBitmap和CBitmap对象通过Detach()函数进行分离 

 

HBITMAP  CMyClass::Load()
{
	CBitmap bmp;
	bmp.LoadBitmap(IDB_BITMAP);

	// 通过Detach 将资源与对象分离,这样bmp析构后,资源仍存在  
	// 否则 ,bmp析构时,会将位图资源一起析构掉,这样出了局部范围外,就不可再使用这个位图资源了
	return bmp.Detach();
}


 

6 在仅获得HBITMAP资源句柄情况下,如何获得这个资源的BITMAP信息

 

		BITMAP bm;
		GetObject(hBitmap,sizeof(BITMAP),&bm);


 

 7 在内存中开辟资源空间 将原图保存到内存中

 

//-------------------在内存中建立区域以存放所得位图-------------------
// 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;

}


 

	ON_MESSAGE(WM_PIC,OnPic)

 

(theApp.m_pMainWnd)->PostMessage(WM_PIC,(WPARAM)CopyHBitmap(hBitmap,IMG_HDC),0);

 

void OnPic(HBITMAP hBitmap)


 

 

你可能感兴趣的:(image,api,null,Class,pascal)