The usage of image processing class CBitmap in VC + + _c language

Source: Internet
Author: User

Use of image processing class CBitmap in VC + +

Class Cbitmap:public CGdiObject {declare_dynamic (cbitmap) public:static cbitmap* PASCAL fromhandle (HBITMAP hBitm

AP);

  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, Lpcolorm
AP 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;
  Must be the 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 load bitmap resources for imported projects

Mount bitmap

  cbitmap bmp;
  Bmp. LoadBitmap (IDB_BITMAP);

2 Loading bitmap files

In order for CBitmap to be able to load bitmap files, API functions must be invoked 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
);

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 to the CBitmap object after the load

if (hbmp!= NULL) {
    CBitmap *pbmp = Cbitmap::fromhandle (hbmp);
  }

Or

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

3 displaying bitmaps

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 Resources

CBitmap bmp;
  Bmp. LoadBitmap (IDB_BITMAP);

  CBitmap *pold=pdc->selectobject (&bmp);

  The bitmap object is also in the PDC, so it is not immediately possible to delete
  //rather than select the bitmap from the DC before deleting
  pdc->selectobject (pold);
  Bmp. DeleteObject ();

5 CBitmap Deconstruction

When CBitmap acts as a local variable after it exits the scope, a destructor occurs, and CBitmap will release its corresponding bitmap resource (HBITMAP).

If you want to continue using the bitmap resource hbitmap, you should detach the bitmap resource Hbitmap and CBitmap objects through the detach () function before exiting the scope

Copy Code code as follows:
Hbitmap Cmyclass::load ()
{
CBitmap bmp;
Bmp. LoadBitmap (IDB_BITMAP);

The resource is separated from the object by detach, so the resource still exists after the BMP destructor
Otherwise, BMP destructor, the bitmap resources will be destructor together, so out of the local scope, you can no longer use this bitmap resources
return BMP. Detach ();
}

6 How to obtain bitmap information for this resource when only HBITMAP resource handles are obtained

BITMAP BM;
GetObject (Hbitmap,sizeof (BITMAP), &BM);

7 in memory to open up resource space to save the original image in memory

-------------------Create a region in memory to hold the resulting bitmap-------------------
//hbitmapsrc for the CBitmap saved rectangle original resource handle
//HDC  
handle/ /In memory to open bitmap resources to save the original
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;

}

Here is a concrete example: save an image from a CBitmap class to a file

Using the following code, you can save images in the CBitmap class to an image file. Support formats: BMP, JPG, GIF, and PNG. 
 
void Savebitmap (CString strFilePath, CBitmap Bitmap)
{
   if (bitmap.m_hobject)
   {
      CImage imgtemp;   CImage is a class in MFC.
      Imgtemp.attach (Bitmap.operator hbitmap ());
      Imgtemp.save (strFilePath);
   } 
 
Note The file pathname strFilePath must contain a suffix, which is one of the BMP, JPG, GIF, or PNG.

Finally attach Cbitmap,hbitmap,bitmap difference and contact

Load a bit diagram, you can use LoadImage:

HANDLE LoadImage (hinstance hinst,lpctstr lpszname,uint utype,int);

LoadImage can be used to load bitmaps, icons, and cursors

Loading can specify the mapping of the load graph to the size of memory:

Cxdesired: Specifies the width, in pixels, of an icon or cursor. If this parameter is zero and lr_defaultsize is not used in the parameter Fuload value, then the function uses the current resource width.

Cydesired: Specifies the height, in pixels, of an icon or cursor. 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 the LoadImage is the handle to the related resource. Because the bitmap is loaded, the returned handle is HBITMAP (requires casting).

Extended Understanding Hbitmap/cbitmap/bitmap:

Hbitmap is the bitmap pointer,

MSDN: Handle to a bitmap.typedef Handle hbitmap;

CBitmap is the class that encapsulates bitmap in MFC;

MSDN:

Encapsulates (includes) a Windows graphics Device interface (GDI) bitmap and provides member functions to manipulate (operations) the BITM Ap.

Bitmap is a structural body that encapsulates some of the information bitmap. Defines the height, width, color format, and bit value of a logical bitmap.

MSDN: This structure defines the type, width, height, color format, and bit values of a bitmap.

The transformation of the relationship between the three:

Hbitmap Hbitmap;

CBitmap Bitmap;

BITMAP BM;

Here are the links between the three:

Bitmap. Attach (HBITMAP)//by HBITMAP to be associated CBitmap

Bitmap. Getbitmap (&BM); The bitmap that is connected by CBitmap
hbitmap= (HBITMAP) bitmap. Getsafehandle ()//by CBitmap to get related hbitmap

The bitmap structure has the following form:

typedef struct TAGBITMAP
{
int bmtype;
int bmwidth;//Width
int bmheight;//High
int bmwidthbytes;
BYTE Bmplanes;
BYTE Bmbitspixel;
LPVOID bmbits;
} BITMAP;

Extended understanding under Attach/detach:

  Attach Associates a C + + object with a Windows object until the association is removed with detach.  
  If the attach does not detach, the Windows object will be ruined by the time the C + + object is destroyed.  
  Attach, the pointer to a C + + object and the HWND of the Windows object have a mapping relationship, which acts as if you were to create a Windows object directly with a C + + object, such as    cedit   edit;   edit.create (...)  
  And this mapping is permanent, knowing that this object is finished.  
  If you use a similar GetDlgItem function, you can return a pointer, and you can cast it. GetDlgItem will look for it in the mapping list.  
  There are 2 kinds of mapping tables, one is permanent and one is temporary.  
  The mapping relationship of a Windows object created directly with a C + + object or a attach object is placed in a permanent table, otherwise the mapping is created in a temporary table.  
  Therefore GetDlgItem does not recommend you to save the returned pointer because it is difficult to ensure that the association of your Windows objects with C + + objects is placed in a permanent table.  
  If the map is placed in a temporary table, it is automatically deleted at idle time.  
  with ATTCAH is entirely to facilitate the use of MFC class member functions to manipulate Windows objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.