位图显示

1,位图

 对于24位以上的和以下的位图头的构造不同:

   >24:总长= sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + Width*Height*4;
  BITMAPFILEHEADER* hdr = (BITMAPFILEHEADER*)m_pBuf;
  BITMAPINFOHEADER *lpbmi = (BITMAPINFOHEADER *)(m_pBuf + sizeof(BITMAPFILEHEADER));
  hdr->bfType = (WORD)'MB';//0x4D42;
  hdr->bfSize = (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+m_nPicHeight*m_nPicWidth*4); 
  hdr->bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER));
  lpbmi->biBitCount = 32;//每个像素字节大小  lpbmi->biCompression = BI_RGB;  lpbmi->biHeight = m_nPicHeight;//高度  lpbmi->biPlanes = 1;  lpbmi->biSize = sizeof(BITMAPINFOHEADER);
  lpbmi->biSizeImage = 0  lpbmi->biWidth = m_nPicWidth;//宽度
<24:就有颜色信息长度在里面,头的长度有BITMAPINFO而不是BITMAPINFOHEADER

 

2,位图显示在对话框中

 HDC hmemdcBK  = ::CreateCompatibleDC(hdc);  //创建内存DC
 HGDIOBJ hOldBK;
 hOldBK = ::SelectObject(hmemdcBK,m_hBmpFigBg);   //把位图选进内存DC
 ::BitBlt(hdc, rcWin.left, rcWin.top, rcWin.Width(), rcWin.Height(), hmemdcBK, 0, 0, SRCCOPY);//显示
 ::SelectObject(hmemdcBK, hOldBK);      //还原老的位图
 ::DeleteDC(hmemdcBK);            //释放DC

 //如果要缩放的话 之前要先把m_hBmpFigBg缩放.

  HDC hDc = GetDC()->m_hDC;
 HDC hMemDCOri = ::CreateCompatibleDC(hDc);
 HDC hMemDCNew = ::CreateCompatibleDC(hDc);
 HGDIOBJ hOld1 = ::SelectObject(hMemDCOri,m_hBmpBg);
 m_hBmpFigBg = ::CreateCompatibleBitmap(hDc,m_nDlgWidth,m_nDlgHeight);
 HGDIOBJ hOld2 = ::SelectObject(hMemDCNew,m_hBmpFigBg);//把位图选进内存DC
 BITMAP bm;
 ::GetObject(m_hBmpBg,sizeof(bm),&bm);
 int nMode = SetStretchBltMode (hMemDCNew, HALFTONE);
 ::StretchBlt(hMemDCNew,0,0,Width,Height,hMemDCOri,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);

//改变了新的内存DC,也就是改变了位图的大小.

你可能感兴趣的:(bmp位图)