Visual C++中DDB与DIB位图编程全攻略
1. 基本概念
BEGIN_MESSAGE_MAP(CBitMapExampleDlg, CDialog) |
3. DDB位图编程
先看DDB加载按钮的单击事件代码:
void CBitMapExampleDlg::OnLoadddbpic() { 1: CBitmap bmpDraw; 2: bmpDraw.LoadBitmap( IDB_LOADED_BITMAP );//装入要加载的DDB位图 3: BITMAP bmpInfo; 4: bmpDraw.GetBitmap( &bmpInfo ); //获取要加载DDB位图的尺寸 5: CDC memDC;//定义一个兼容DC 6: CClientDC dc( this ); 7: memDC.CreateCompatibleDC( &dc );//创建兼容DC 8: CBitmap* pbmpOld = memDC.SelectObject( &bmpDraw );//保存原有DDB,并选入新DDB入DC 9: dc.BitBlt( 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &memDC, 0, 0, SRCCOPY ); 10: memDC.SelectObject( pbmpOld );//选入原DDB } |
图1 加载DDB位图资源 |
/* Bitmap Header Definition */ typedef struct tagBITMAP { LONG bmType; //必需为0 LONG bmWidth; //位图的宽度(以像素为单位) LONG bmHeight; //位图的高度(以像素为单位) LONG bmWidthBytes; //每一扫描行所需的字节数,应是偶数 WORD bmPlanes; //色平面数 WORD bmBitsPixel; //色平面的颜色位数 LPVOID bmBits; //指向存储像素阵列的数组 } BITMAP, *PBITMAP, NEAR *NPBITMAP, FAR *LPBITMAP; |
CDC::BitBlt(int x, int y, int nWidth, int nHeight, CDC *pSrcDC, int xSrc, int ySrc, DWORD dwRop) |
BOOL CDC::StretchBlt(int x, int y, int nWidth, int nHeight, CDC *pSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop); |
CRect clientRect; GetClientRect(&clientRect); //获得对话框窗口的大小 dc.StretchBlt(0, 0, clientRect.right, clientRect.bottom, &memDC, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, SRCCOPY); |
void CBitMapExampleDlg::OnMarkDdbpic() { CBitmap bmpDraw; bmpDraw.LoadBitmap(IDB_YESKY_BITMAP); //装入天极网logo DDB位图资源 BITMAP bmpInfo; bmpDraw.GetBitmap(&bmpInfo); //获取天极网logo位图的尺寸 CDC memDC; //定义一个兼容DC CClientDC dc(this); memDC.CreateCompatibleDC(&dc); //创建DC CBitmap *pbmpOld = memDC.SelectObject(&bmpDraw); //保存原有DDB,并选入天极网logo位图入DC dc.BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &memDC, 0, 0, SRCAND); memDC.SelectObject(pbmpOld); //选入原DDB } |
图3 在DDB位图中加入天极网logo |
dc.BitBlt ( 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &memDC, 0, 0, SRCAND ); |
typedef struct tagBITMAPFILEHEADER { WORD bfType; //文件类型,必须是0x424D,即字符串"BM" DWORD bfSize; //文件大小,包括BITMAPFILEHEADER的14个字节 WORD bfReserved1; //保留字 WORD bfReserved2; //保留字 DWORD bfOffBits; //从文件头到实际的位图数据的偏移字节数 } BITMAPFILEHEADER; |
typedef struct tagBITMAPINFOHEADER { DWORD biSize; //本结构的长度,为40 LONG biWidth; //图象的宽度,单位是象素 LONG biHeight; //图象的高度,单位是象素 WORD biPlanes; //必须是1 WORD biBitCount; //表示颜色时要用到的位数,1(单色), 4(16色), 8(256色), 24(真彩色) DWORD biCompression; //指定位图是否压缩,有效的值为BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS等,BI_RGB表示不压缩 DWORD biSizeImage; //实际的位图数据占用的字节数,即 biSizeImage=biWidth’ × biHeight,biWidth’是biWidth 按照4的整倍数调整后的结果 LONG biXPelsPerMeter; //目标设备的水平分辨率,单位是每米的象素个数 LONG biYPelsPerMeter; //目标设备的垂直分辨率,单位是每米的象素个数 DWORD biClrUsed; //位图实际用到的颜色数,0表示颜色数为2biBitCount DWORD biClrImportant; //位图中重要的颜色数,0表示所有颜色都重要 } BITMAPINFOHEADER; |
typedef struct tagRGBQUAD { BYTE rgbBlue; //蓝色分量 BYTE rgbGreen; //绿色分量 BYTE rgbRed; //红色分量 BYTE rgbReserved; //保留值 } RGBQUAD; |
// DIB.h:类CDib声明头文件 #ifndef __DIB_H__ #define __DIB_H__ #include <wingdi.h> class CDib { public: CDib(); ~CDib(); BOOL Load( const char * ); BOOL Save( const char * ); BOOL Draw( CDC *, int nX = 0, int nY = 0, int nWidth = -1, int nHeight = -1, int mode = SRCCOPY); BOOL SetPalette( CDC * ); private: CPalette m_Palette; unsigned char *m_pDib, *m_pDibBits; DWORD m_dwDibSize; BITMAPINFOHEADER *m_pBIH; RGBQUAD *m_pPalette; int m_nPaletteEntries; }; #endif |
// DIB.cpp:类CDib实现文件 #include "stdafx.h" #include "DIB.h" CDib::CDib() { m_pDib = NULL; } CDib::~CDib() { // 如果位图已经被加载,释放内存 if (m_pDib != NULL) delete []m_pDib; } |
BOOL CDib::Load(const char *pszFilename) { CFile cf; // 打开位图文件 if (!cf.Open(pszFilename, CFile::modeRead)) return (FALSE); // 获得位图文件大小,并减去BITMAPFILEHEADER的长度 DWORD dwDibSize; dwDibSize = cf.GetLength() - sizeof(BITMAPFILEHEADER); // 为DIB位图分配内存 unsigned char *pDib; pDib = new unsigned char[dwDibSize]; if (pDib == NULL) return (FALSE); BITMAPFILEHEADER BFH; // 读取位图文件数据 try { // 文件格式是否正确有效 if ( cf.Read(&BFH, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER) || BFH.bfType != ’MB’ || cf.Read(pDib, dwDibSize) != dwDibSize) { delete []pDib; return (FALSE); } } catch (CFileException *e) { e->Delete(); delete []pDib; return (FALSE); } // delete先前加载的位图 if (m_pDib != NULL) delete m_pDib; // 将临时Dib数据指针和Dib大小变量赋给类成员变量 m_pDib = pDib; m_dwDibSize = dwDibSize; // 为相应类成员变量赋BITMAPINFOHEADER和调色板指针 m_pBIH = (BITMAPINFOHEADER*)m_pDib; m_pPalette = (RGBQUAD*) &m_pDib[sizeof(BITMAPINFOHEADER)]; // 计算调色板中实际颜色数量 m_nPaletteEntries = 1 << m_pBIH->biBitCount; if (m_pBIH->biBitCount > 8) m_nPaletteEntries = 0; else if (m_pBIH->biClrUsed != 0) m_nPaletteEntries = m_pBIH->biClrUsed; // 为相应类成员变量赋image data指针 m_pDibBits = &m_pDib[sizeof(BITMAPINFOHEADER) + m_nPaletteEntries * sizeof (RGBQUAD)]; // delete先前的调色板 if (m_Palette.GetSafeHandle() != NULL) m_Palette.DeleteObject(); // 如果位图中存在调色板,创建LOGPALETTE 及CPalette if (m_nPaletteEntries != 0) { LOGPALETTE *pLogPal = (LOGPALETTE*)new char[sizeof(LOGPALETTE) + m_nPaletteEntries *sizeof(PALETTEENTRY)]; if (pLogPal != NULL) { pLogPal->palVersion = 0x300; pLogPal->palNumEntries = m_nPaletteEntries; for (int i = 0; i < m_nPaletteEntries; i++) { pLogPal->palPalEntry[i].peRed = m_pPalette[i].rgbRed; pLogPal->palPalEntry[i].peGreen = m_pPalette[i].rgbGreen; pLogPal->palPalEntry[i].peBlue = m_pPalette[i].rgbBlue; } //创建CPalette并释放LOGPALETTE的内存 m_Palette.CreatePalette(pLogPal); delete []pLogPal; } } return (TRUE); } //函数功能:保存位图入BMP文件 BOOL CDib::Save(const char *pszFilename) { if (m_pDib == NULL) return (FALSE); CFile cf; if (!cf.Open(pszFilename, CFile::modeCreate | CFile::modeWrite)) return (FALSE); try { BITMAPFILEHEADER BFH; memset(&BFH, 0, sizeof(BITMAPFILEHEADER)); BFH.bfType = ’MB’; BFH.bfSize = sizeof(BITMAPFILEHEADER) + m_dwDibSize; BFH.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + m_nPaletteEntries *sizeof(RGBQUAD); cf.Write(&BFH, sizeof(BITMAPFILEHEADER)); cf.Write(m_pDib, m_dwDibSize); } catch (CFileException *e) { e->Delete(); return (FALSE); } return (TRUE); } |
BOOL CDib::Draw(CDC *pDC, int nX, int nY, int nWidth, int nHeight, int mode) { if (m_pDib == NULL) return (FALSE); // 获取位图宽度和高度赋值 if (nWidth == - 1) nWidth = m_pBIH->biWidth; if (nHeight == - 1) nHeight = m_pBIH->biHeight; // 绘制位图 StretchDIBits(pDC->m_hDC, nX, nY, nWidth, nHeight, 0, 0, m_pBIH->biWidth, m_pBIH->biHeight, m_pDibBits, (BITMAPINFO*)m_pBIH, BI_RGB, mode); return (TRUE); } //函数功能:设置调色板 BOOL CDib::SetPalette(CDC *pDC) { if (m_pDib == NULL) return (FALSE); // 检查当前是否有一个调色板句柄,对于大于256色的位图,为NULL if (m_Palette.GetSafeHandle() == NULL) return (TRUE); // 选择调色板,接着实施之,最后恢复老的调色板 CPalette *pOldPalette; pOldPalette = pDC->SelectPalette(&m_Palette, FALSE); pDC->RealizePalette(); pDC->SelectPalette(pOldPalette, FALSE); return (TRUE); } |
m_hBitmap = ::CreateDIBSection(pDC->GetSafeHdc(), (LPBITMAPINFO) m_lpBMPHdr, DIB_RGB_COLORS, (LPVOID*) &m_lpDIBits, NULL, 0); |
HBITMAP m_hBitmap; |
void CBitMapExampleDlg::OnOpendibpic() { // 弹出文件对话框,让用户选择位图文件 CFileDialog fileDialog(TRUE, "*.BMP", NULL, NULL,"位图文件(*.BMP)|*.bmp;*.BMP|"); if (IDOK == fileDialog.DoModal()) { // 加载位图并显示之 CDib dib; if (dib.Load(fileDialog.GetPathName())) { CClientDC dc(this); dib.SetPalette(&dc); dib.Draw(&dc); } } } |
void CBitMapExampleDlg::OnMarkDibpic() { // 弹出文件对话框,让用户选择标记logo CFileDialog fileDialog(TRUE, "*.BMP", NULL, NULL, "标记位图文件(*.BMP)|*.bmp;*.BMP|"); if (IDOK == fileDialog.DoModal()) { // 加载标记logo位图并与目标位图相与 CDib dib; if (dib.Load(fileDialog.GetPathName())) { CClientDC dc(this); dib.SetPalette(&dc); dib.Draw(&dc, 0, 0, - 1, - 1, SRCAND); } } } |
图4 在DIB位图中加入天极网logo |