VC++中图像处理类CBitmap的用法

2020-01-06 13:59:45王旭

LoadImage可以用来加载位图,图标和光标

加载时可以规定加载图的映射到内存的大小:

    cxDesired:指定图标或光标的宽度,以像素为单位。如果此参数为零并且参数fuLoad值中LR_DEFAULTSIZE没有被使用,那么函数使用目前的资源宽度。

 cyDesired:指定图标或光标的高度,以像素为单位。如果此参数为零并且参数fuLoad值中LR_DEFAULTSIZE没有被使用,那么函数使用目前的资源高度。

LoadImage的返回值是相关资源的句柄。因为加载的是位图所以返回的句柄是HBITMAP型的(需要强制转换)。

延伸理解 HBITMAP/CBitmap/BITMAP:

HBITMAP是bitmap的指针,

msdn中如是:Handle to a bitmap.typedef HANDLE HBITMAP;

CBitmap是mfc中封装bitmap的类;

msdn中:

Encapsulates(囊括) a Windows graphics device interface (GDI) bitmap and provides member functions to manipulate(操作) the bitmap.

BITMAP是一个结构体,封装着bitmap的一些信息。定义了逻辑位图的高,宽,颜色格式和位值。

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

三者之间的关系转换:

HBITMAP hBitmap;

CBitmap bitmap;

BITMAP bm;

//下面是三者之间的联系:

bitmap.Attach(hBitmap);//由HBITMAP 得到关联的CBitmap

bitmap.GetBitmap(&bm); // 由CBitmap 得到关联的BITMAP 
hBitmap=(HBITMAP)bitmap.GetSafeHandle();//由CBitmap得到相关的HBITMAP

BITMAP结构具有如下形式:

typedef struct tagBITMAP

     int      bmType;
     int      bmWidth;//宽
     int      bmHeight;//高
     int      bmWidthBytes;
     BYTE     bmPlanes;