MFC如何显示位图

 

继续昨天的工作!开始以为不能在BitBlt()中使图片以其自己的大小显示!我昏了!今天又不得不打开以前看的书的继续学习!重新写了这个函数!

void CBitmapView::wtj_paint()
{
  CDC *pDC;
  pDC=GetDC();
  CBitmap wtj_bitmap;
  CDC dcMemory;
  //?CDC *pDC;
  wtj_bitmap.LoadBitmap(IDB_BITMAP1);
  BITMAP bmInfo;
  wtj_bitmap.GetObject(sizeof(bmInfo),&bmInfo);
  dcMemory.CreateCompatibleDC(pDC);
  dcMemory.SelectObject(&wtj_bitmap);
  pDC->BitBlt(100,100,
 bmInfo.bmWidth,??bmInfo.bmHeight,
  &dcMemory,0,0,SRCCOPY);
 ReleaseDC(pDC);
}
其中我还是不大明白,为什么wtj_bitmap.GetObject()函数可以这样用,可以说是把wtj_bitmap的信息copy到了bmInfo中。昨天我都还在想,怎样才能把位图的长和宽的信息取出来!哦解决了!

简单的记一下如何显示位图:

1、声明一个CBitmap对象,并从资源中加位图。

2、创建一个兼容的DC,用CDC的成员函数CreateCompatibleDC。

3、用CDC的成员函数SelectObject将位图选入到兼容DC中。

4、用CDC的成员函数BitBlt来输出位图。

就这样了,一个完整的位图说显示了!

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

如下的改动可以使位图在窗口中全部显示,自动拉伸!

void CBitmapView::wtj_paint()
{
  CDC *pDC;
  CRect rect;
  GetWindowRect(rect);
//GetClientRect(rect);

  pDC=GetDC();
 
CBitmap wtj_bitmap;
  CDC dcMemory;
//CDC *pDC;
  wtj_bitmap.LoadBitmap(IDB_BITMAP1);
  BITMAP bmInfo;
  wtj_bitmap.GetObject(sizeof(bmInfo),&bmInfo);
  dcMemory.CreateCompatibleDC(pDC);
  dcMemory.SelectObject(&wtj_bitmap);
  pDC->StretchBlt(0,0,
  rect.Width(),rect.Height(),
  &dcMemory,
  0,0,
  bmInfo.bmWidth,bmInfo.bmHeight,
  SRCCOPY);
//pDC->
/*
  pDC->BitBlt(100,100,
  bmInfo.bmWidth,??bmInfo.bmHeight,
  &dcMemory,0,0,SRCCOPY);
*/
ReleaseDC(pDC);
}

其中红色部份,书上说的很清楚,一个是取得窗口客户区,一个是取得窗口活动区域!两者有什么区别呢?在实际的应用中没有体现出来!

**一点小的问题,我看到书上说明,在Windows中显示位图,与计算机的硬件是相关的,因为不同的显示器,不同的显示卡会有不同的结果,更头疼的可能是要在打印机上实现,都会有一些问题!希望在以后的学习中自己总结一下,也参考一些别人成功的例子!就MFC这一些,技术还是比较成熟的!

如何打开一个BMP文件呢?

再来改进这断代码:

void CBitmapView::wtj_paint()
{
  CDC *pDC;
  CRect rect;
  GetWindowRect(rect);
//GetClientRect(rect);
  pDC=GetDC();
  CBitmap wtj_bitmap;
  CDC dcMemory;
//CDC *pDC;

//wtj_bitmap.LoadBitmap(IDB_BITMAP1);

  HBITMAP hbitmap=(HBITMAP)LoadImage(NULL,
  m_bmPath,
  IMAGE_BITMAP,
  0,0,
  LR_LOADFROMFILE);
  wtj_bitmap.Attach(hbitmap);
  BITMAP bmInfo;
  wtj_bitmap.GetObject(sizeof(bmInfo),&bmInfo);
  dcMemory.CreateCompatibleDC(pDC);
  dcMemory.SelectObject(&wtj_bitmap);
  pDC->StretchBlt(0,0,
  rect.Width(),rect.Height(),
  &dcMemory,
  0,0,
  bmInfo.bmWidth,bmInfo.bmHeight,
  SRCCOPY);
//pDC->
/*
  pDC->BitBlt(100,100,
  bmInfo.bmWidth,??bmInfo.bmHeight,
  &dcMemory,0,0,SRCCOPY);
*/?
 ReleaseDC(pDC);
}
查看了一下LoadImage()的用法,有点怪,好象不是MFC中的函数,因为在MSDN中有没有发现这个函数的说明,有的只是一个CToolBarCtrlCToolBarCtrl类的成员函数!

终于找到了:

The LoadImage function loads an icon, cursor, or bitmap.

HANDLE LoadImage(
  HINSTANCE hinst,   // handle of the instance containing the image
  LPCTSTR lpszName,  // name or identifier of image
  UINT uType,        // type of image
  int cxDesired,     // desired width
  int cyDesired,     // desired height
  UINT fuLoad        // load flags
);
如果载入时出错会怎样呢?
if(hbitmap){
  ...
}else{
  ...
}
或许有用!
If the function succeeds, the return value is the handle of the newly loaded image.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
好了,明天继续吧!
原文地址:https://www.cnblogs.com/huqingyu/p/44631.html