近日要实现将缩小的位图保存在后台,以便在OnPaint刷新的时候仍然可以看到正确的图像,遂在lg_Bitmap类中添加了这样一个函数
BOOL lg_Bitmap::LoadFromHDC(HDC hDC)
}
然后在外部如此调用
CDC * pScreenDC = new CDC;
pScreenDC->CreateCompatibleDC(pDC);
CBitmap TempBitmap;
TempBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
CBitmap* pOldScreenDC = NULL;
pOldScreenDC = (CBitmap*)pScreenDC->SelectObject(&TempBitmap);
pScreenDC->StretchBlt(0,0,rect.Width(),rect.Height(),pMemDC,0,0,BIT.bmWidth,BIT.bmHeight,SRCCOPY);
DrawTxtInDC(pScreenDC,5,15,topinfo,strPosition,rect.Height());
if(g_ScreenBitmap.LoadFromHDC(pScreenDC->GetSafeHdc()))
delete pScreenDC;
再然后界面显示了一张这样的图

我就郁闷了,好好的图咋整这样了呢?开始找原因
由于位图类中没有提供保存位图的功能,遂再增加两个函数
1,保存lg_Bitmap自身的图 2,保存指定LPBITMAPINFOHEADER,和缓冲的图
//不支持调色板
BOOL lg_Bitmap::SaveThis(LPCTSTR fn)
}
然后在lg_Bitmap::LoadFromHDC(HDC hDC)的结尾加入这么一段
lg_TimeString Tsing;
string sPath = Tsing.Format("e:\抓图\%N%Y%R%S%F%M%H.bmp");
CreateAllDirectory(sPath.c_str());
// SaveThis(sPath.c_str());
Save(&bih,pTemp,sPath.c_str());
结果无论是lg_Bitmap自身还是外部传来的位图无一例外的变成了上面那个图的样子
..............思考中,无意中在google上看到一句话,在使用StretchBlt缩小位图的时候要SetStretchBltMode(COLORONCOLOR).......我晕,管他先试试。
CDC * pScreenDC = new CDC;
pScreenDC->CreateCompatibleDC(pDC);
CBitmap TempBitmap;
TempBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
CBitmap* pOldScreenDC = NULL;
pOldScreenDC = (CBitmap*)pScreenDC->SelectObject(&TempBitmap);
pScreenDC->SetStretchBltMode(COLORONCOLOR);
pScreenDC->StretchBlt(0,0,rect.Width(),rect.Height(),pMemDC,0,0,BIT.bmWidth,BIT.bmHeight,SRCCOPY);
DrawTxtInDC(pScreenDC,5,15,topinfo,strPosition,rect.Height());
if(g_ScreenBitmap.LoadFromHDC(pScreenDC->GetSafeHdc()))
delete pScreenDC;
结果

相关文章: