近日要实现将缩小的位图保存在后台,以便在OnPaint刷新的时候仍然可以看到正确的图像,遂在lg_Bitmap类中添加了这样一个函数

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)    BOOL lg_Bitmap::LoadFromHDC(HDC hDC)
    }

然后在外部如此调用

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        CDC * pScreenDC = new CDC;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        pScreenDC
->CreateCompatibleDC(pDC);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        CBitmap TempBitmap;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        TempBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        CBitmap
* pOldScreenDC = NULL;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        pOldScreenDC 
= (CBitmap*)pScreenDC->SelectObject(&TempBitmap);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        pScreenDC->StretchBlt(0,0,rect.Width(),rect.Height(),pMemDC,0,0,BIT.bmWidth,BIT.bmHeight,SRCCOPY);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        DrawTxtInDC(pScreenDC,
5,15,topinfo,strPosition,rect.Height());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        
if(g_ScreenBitmap.LoadFromHDC(pScreenDC->GetSafeHdc()))
        delete pScreenDC;

再然后界面显示了一张这样的图
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)
我就郁闷了,好好的图咋整这样了呢?开始找原因
由于位图类中没有提供保存位图的功能,遂再增加两个函数
1,保存lg_Bitmap自身的图 2,保存指定LPBITMAPINFOHEADER,和缓冲的图

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)    //不支持调色板
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)
    BOOL lg_Bitmap::SaveThis(LPCTSTR fn)
    }

 

然后在lg_Bitmap::LoadFromHDC(HDC hDC)的结尾加入这么一段

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        lg_TimeString Tsing;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        
string sPath = Tsing.Format("e:\抓图\%N%Y%R%S%F%M%H.bmp");
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        CreateAllDirectory(sPath.c_str());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)
//        SaveThis(sPath.c_str());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)
        Save(&bih,pTemp,sPath.c_str());

结果无论是lg_Bitmap自身还是外部传来的位图无一例外的变成了上面那个图的样子
..............思考中,无意中在google上看到一句话,在使用StretchBlt缩小位图的时候要SetStretchBltMode(COLORONCOLOR).......我晕,管他先试试。

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        CDC * pScreenDC = new CDC;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        pScreenDC
->CreateCompatibleDC(pDC);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        CBitmap TempBitmap;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        TempBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        CBitmap
* pOldScreenDC = NULL;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        pOldScreenDC 
= (CBitmap*)pScreenDC->SelectObject(&TempBitmap);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        pScreenDC
->SetStretchBltMode(COLORONCOLOR);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        pScreenDC
->StretchBlt(0,0,rect.Width(),rect.Height(),pMemDC,0,0,BIT.bmWidth,BIT.bmHeight,SRCCOPY);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        DrawTxtInDC(pScreenDC,
5,15,topinfo,strPosition,rect.Height());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)        
if(g_ScreenBitmap.LoadFromHDC(pScreenDC->GetSafeHdc()))
        delete pScreenDC;

结果
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)

相关文章: