The usage of image processing class CBitmap in VC++
- 2020-05-05 11:39:08
- OfStack
The usage of CBitmap in VC++
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
};
Father
CGdiObject
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
};
Load the bitmap resource
of the imported project
// Loads a bitmap
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP);
Load the bitmap file
must call the API function LoadImage
in order for CBitmap to be able to load bitmap files
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
);
Load: 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);
Connect the HBITMAP resource handle obtained after loading to CBitmap object
if (hBmp != NULL) {
CBitmap *pBmp = CBitmap::FromHandle(hBmp);
}
Or
CBitmap bmp;
if (hBmp != NULL) {
bmp.DeleteObject();
bmp.Attach(hBmp);
}
3 displays bitmap
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 delete resource
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP);
CBitmap *pOld=pDC->SelectObject(&bmp);
// The bitmap object is still there pDC , so it cannot be deleted immediately
// Instead, start with the bitmap DC selected And then delete it
pDC->SelectObject(pOld);
bmp.DeleteObject();
5 CBitmap destructor
When CBitmap, as a local variable, exits its scope, destruction occurs, and CBitmap releases its corresponding bitmap resource (hBitmap).
To continue using the bitmap resource hBitmap, separate the bitmap resource hBitmap and CBitmap objects through the Detach() function
before exiting the scope
HBITMAP CMyClass::Load()
{
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP);
// through Detach Separate the resource from the object, so bmp After destruction, the resource still exists
// Otherwise, . bmp When destructing, the bitmap resource is destructed together, so that out of the local scope, the bitmap resource can no longer be used
return bmp.Detach();
}
6 how do I get BITMAP information
for HBITMAP resource if I only get the HBITMAP resource handle
BITMAP bm;
GetObject(hBitmap,sizeof(BITMAP),&bm);
7 create resource space in memory and save the original image to
in memory
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
};
0
Here's an example: saves images from the CBitmap class to the file
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
finally attached CBitmap,HBitmap,Bitmap distinction and contact
To load a one-bit graph, you can use LoadImage:
HANDLE LoadImage (HINSTANCE hinst, LPCTSTR lpszName, UINT uType, int cxDesired, int CyDesired, UINT fuLoad);
LoadImage can be used to load bitmaps, ICONS and the cursor
The size of the map to memory of the load graph can be specified at load time:
cxDesired: specifies the width of the icon or cursor, in pixels. If this parameter is zero and LR_DEFAULTSIZE is not used in the fuLoad value, the function USES the current resource width.
cyDesired: specifies the height of the icon or cursor, in pixels. If this parameter is zero and LR_DEFAULTSIZE is not used in the parameter fuLoad value, then the function USES the current resource height.
The return value of LoadImage is a handle to the relevant resource. Because the bitmap is loaded, the returned handle is of type HBITMAP (cast).
Extended understanding of HBITMAP/CBitmap/BITMAP:
HBITMAP is a pointer to bitmap,
msdn: Handle to a bitmap typedef HANDLE HBITMAP;
CBitmap is the class that encapsulates bitmap in mfc.
: in the msdn
Encapsulates (including) a Windows device interface
BITMAP is a structure that encapsulates some information about bitmap. Defines the height, width, color format, and bit values of a logical bitmap.
MSDN: This structure defines type, width, height, color format, and bit of bitmap
The relationship between the three:
HBITMAP hBitmap;
CBitmap bitmap;
BITMAP bm;
// the following is the connection between the three:
bitmap. Attach (hBitmap); // the associated CBitmap
is obtained by HBITMAP
bitmap. GetBitmap (& bm); // the associated BITMAP
is obtained by CBitmap
hBitmap = (HBITMAP) bitmap. GetSafeHandle (); // HBITMAP
The BITMAP structure has the following form:
typedef struct tagBITMAP
{
int bmType;
int bmWidth; / /
wide
int bmHeight; / / high
int bmWidthBytes;
BYTE bmPlanes;
BYTE bmBitsPixel;
LPVOID bmBits;
} BITMAP;
Attach/Detach:
attach associates an C++ object with an WINDOWS object until the association is removed with detach.
if attach does not have detach after attach, C++ object is destroyed when WINDOWS object is destroyed.
After attach is finished, there will be a mapping between the pointer of C++ object and HWND of WINDOWS object. edit. create (...).
and the mapping is permanent until the object dies.
can also return a pointer if it USES a function similar to GetDlgItem and can be cast. GetDlgItem is going to look in the table.
has two mapping tables, one permanent and one temporary.
objects created directly from C++ objects or the mapping of attach objects are placed in the permanent table, otherwise the mapping is created in the temporary table.
so GetDlgItem does not recommend that you save the returned pointer because it is difficult to ensure that your WINDOWS object is in the permanent table with the C++ object.
is automatically deleted in idle time if the map is placed in a temporary table.
USES attcah solely for the convenience of manipulating WINDOWS objects with the member functions of the MFC class.