在基于单文档的MFC下怎样显示BMP图片
已经把原始数据转换为256色的BMP格式的图像返回的是储存BMP图象信息及位数据的内存块句柄现在如何通过这个句柄用OnDraw显示图像?...
已经把原始数据转换为256色的BMP格式的图像 返回的是储存BMP图象信息及位数据的内存块句柄 现在如何通过这个句柄用OnDraw显示图像?
展开
1个回答
展开全部
// This OnDraw() handler loads a bitmap from system resources,
// centers it in the view, and uses BitBlt() to paint the bitmap
// bits.
void CBlat2View::OnDraw(CDC* pDC)
{
CBlat2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// load IDB_BITMAP1 from our resources
CBitmap bmp;
if (bmp.LoadBitmap(IDB_BITMAP1))
{
// Get the size of the bitmap
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
// Create an in-memory DC compatible with the
// display DC we're using to paint
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
// Select the bitmap into the in-memory DC
CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
// Find a centerpoint for the bitmap in the client area
CRect rect;
GetClientRect(&rect);
int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;
// Copy the bits from the in-memory DC into the on-
// screen DC to actually do the painting. Use the centerpoint
// we computed for the target offset.
pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
0, 0, SRCCOPY);
dcMemory.SelectObject(pOldBitmap);
}
else
TRACE0("ERROR: Where's IDB_BITMAP1?\n");
}
MSDN上的一个例子
// centers it in the view, and uses BitBlt() to paint the bitmap
// bits.
void CBlat2View::OnDraw(CDC* pDC)
{
CBlat2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// load IDB_BITMAP1 from our resources
CBitmap bmp;
if (bmp.LoadBitmap(IDB_BITMAP1))
{
// Get the size of the bitmap
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
// Create an in-memory DC compatible with the
// display DC we're using to paint
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
// Select the bitmap into the in-memory DC
CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
// Find a centerpoint for the bitmap in the client area
CRect rect;
GetClientRect(&rect);
int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;
// Copy the bits from the in-memory DC into the on-
// screen DC to actually do the painting. Use the centerpoint
// we computed for the target offset.
pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
0, 0, SRCCOPY);
dcMemory.SelectObject(pOldBitmap);
}
else
TRACE0("ERROR: Where's IDB_BITMAP1?\n");
}
MSDN上的一个例子
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询