MFC中GDI之CBitmap(位图)

时间:2019-11-28
本文章向大家介绍MFC中GDI之CBitmap(位图),主要包括MFC中GDI之CBitmap(位图)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

CBitmap类主要是加载位图资源,或者建立一个空白位图用于存储画面。

BOOL LoadBitamp(UINT nIDResource) 从工程资源中加载一张位图
BOOL LoadOEMBitmap(UINT nIDBitmap) 从系统资源中加载一张位图
BOOL CreateBitmap(int nWidth, int nHeigjt, UINT nPlane, UINT nBitCnt, const void* lpBits) 根据指定的值创建一张位图
BOOL CreateCompatibleBitmap(CDC* pDC, int nWidth, int nHeight) 根据高宽创建一张兼容位图
BOOL CreateBitmapIndirect(LPBITMAP lpBitmap) 根据BITMAP结构体创建一张空白位图
int GetBitamp(BITMAP* pBitmap) 根据BITMAP结构体获取位图属性信息
static CBitmap* FromHandle(HBITMAP hBitmap) 将HBITMAP句柄转换为CBitamp对象
operator HBITMAP() const 从CBitmap对象中获取HBITMAP句柄

BITMAP结构体:

/* Bitmap Header Definition */
typedef struct tagBITMAP
  {
    LONG        bmType;
    LONG        bmWidth;
    LONG        bmHeight;
    LONG        bmWidthBytes;
    WORD        bmPlanes;
    WORD        bmBitsPixel;
    LPVOID      bmBits;
  } BITMAP, *PBITMAP, NEAR *NPBITMAP, FAR *LPBITMAP;

装载位图并显示:

① 位图装载   CBitamp::LoadBitmap
② 创建兼容的内存DC     CDC::CreateCompatibleDC
③ 内存DC选择位图对象    CDC::SelectObject
④ 使用贴图函数显示内存DC中的位图内容    CDC::BitBlt
注:若使用压缩或拉伸原始图片则使用   CDC::StretchBlt     

例子:

void CMFC7_7BitmapDlg::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
    myBitmap(&dc);
}

void CMFC7_7BitmapDlg::myBitmap(CDC *pDC)
{
    CBitmap bmp;
    bmp.LoadBitmap(IDB_WOLF);
    BITMAP bm;   
    bmp.GetBitmap(&bm);// 获得位图的详细信息
    CDC mdc;
    mdc.CreateCompatibleDC(pDC);//创建兼容DC
    mdc.SelectObject(&bmp);//选择位图对象
    pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight, &mdc, 0,0,SRCCOPY);//贴图
    // 压缩或拉伸
    pDC->SetStretchBltMode(HALFTONE); // 设置压缩拉伸方式,效果好
    pDC->StretchBlt(bm.bmWidth,0,bm.bmWidth/2,bm.bmHeight/2, &mdc,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//压缩
    pDC->StretchBlt(bm.bmWidth*2,0,bm.bmWidth*2,bm.bmHeight*2, &mdc,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//拉伸

    pDC->StretchBlt(0,bm.bmHeight,bm.bmWidth,bm.bmHeight, &mdc,bm.bmWidth,0,-bm.bmWidth,bm.bmHeight,SRCCOPY);//左右翻转
    pDC->StretchBlt(0,bm.bmHeight*2,bm.bmWidth,bm.bmHeight, &mdc,0,bm.bmHeight,bm.bmWidth,-bm.bmHeight,SRCCOPY);//上下翻转
    pDC->StretchBlt(0,bm.bmHeight*3,bm.bmWidth,bm.bmHeight, &mdc,bm.bmWidth,bm.bmHeight,-bm.bmWidth,-bm.bmHeight,SRCCOPY);//左右上下翻转

}

原文地址:https://www.cnblogs.com/htj10/p/11953880.html