获取Picture位置及其大小并显示位图

来源:互联网 发布:老男孩知乎 编辑:程序博客网 时间:2024/05/10 13:55

void Student::DrawBMP()
{
 // if we don't have an image, get out of here
  JpegFile jpg;
 
 if (pRGB==NULL) return;//pRGB图像数据
 
 CDC *theDC = GetDC();
 
 if (theDC!=NULL) {
  

  
  // a 24-bit DIB is DWORD-aligned, vertically flipped and
  // has Red and Blue bytes swapped. we already did the
  // RGB->BGR and the flip when we read the images, now do
  // the DWORD-align
  
  BYTE *tmp;
  // DWORD-align for display
  UINT m_widthDW;
  tmp = jpg.MakeDwordAlignedBuf(pRGB,
   m_nWidth,
   m_nHeigth,
   &m_widthDW);
  
  // set up a DIB
  BITMAPINFOHEADER bmiHeader;
  bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bmiHeader.biWidth = m_nWidth;
  bmiHeader.biHeight = m_nHeigth;
  bmiHeader.biPlanes = 1;
  bmiHeader.biBitCount = 24;
  bmiHeader.biCompression = BI_RGB;
  bmiHeader.biSizeImage = 0;
  bmiHeader.biXPelsPerMeter = 0;
  bmiHeader.biYPelsPerMeter = 0;
  bmiHeader.biClrUsed = 0;
  bmiHeader.biClrImportant = 0;

 
 
 CRect cRect;
// CStatic ctrlStatic;//先将picture控件和 一个CStatic变量进行绑定 
 
 //得到控件的客户区
 m_ctrlStatic.GetClientRect(&cRect);
 cRect.NormalizeRect();// 
 m_ctrlStatic.ClientToScreen(&cRect);
 
 //转换到屏幕坐标,其后获取正确的Picture区域大小
 int rHeight = cRect.Height();
 int rWidth  = cRect.Width();
 
 //控件屏幕坐标转换到相对于对话框的客户坐标
 ScreenToClient(&cRect);
 // 之后才能获取准确的 picture控件 与 对话框的相对坐标
 int rPhotoLeft= cRect.left;
 int rPhotoTop = cRect.top ;
 int rPhotoRight  = cRect.right;
    int rPhotoBottom = cRect.bottom;
  // now blast it to the CDC passed in.
  // lines returns the number of lines actually displayed
  int lines = StretchDIBits(theDC->m_hDC,
   rPhotoLeft, rPhotoTop,
  rPhotoRight-rPhotoLeft,
   rPhotoBottom-rPhotoTop,
   0,0,
   bmiHeader.biWidth,
   bmiHeader.biHeight,
   tmp,
   (LPBITMAPINFO)&bmiHeader,
   DIB_RGB_COLORS,
   SRCCOPY);
  
  delete [] tmp;
  
  ReleaseDC(theDC);
  
 }
}

 

//////////////////////////////////////////////////////////////////////////

  

如果不绑定Picture控件也可如下:

 CWnd*   pWnd   =   GetDlgItem(IDC_PICTURE); 

 pWnd->GetClientRect(&recrt);//相对控件本身的坐标recrt.left=0;recrt.top=0; 
 pWnd->GetWindowRect(&rect2);//相对与窗体的坐标 
 pWnd->MoveWindow(0,0,800,600,1);//设置控件的位置和大小

原创粉丝点击