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

摘要:
最近,我们希望将缩小的位图保存在背景中,以便在OnPaint刷新时仍然可以看到正确的图像。因此,在lg_ Bitmap类中添加了这样一个函数BOOLlg_ Bitmap::LoadFromHDC(HDChDC){if(NULL==hDC)returnFALSE;BITMAPBitmap;HBITMAPhBitmap=(HBITMAP)::GetCurrentObject(hDC,OBJ_Bitmap);::

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

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张    BOOL lg_Bitmap::LoadFromHDC(HDC hDC)
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第2张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第3张    
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
if(NULL == hDC)
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            
return FALSE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        BITMAP Bitmap;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        HBITMAP hBitmap 
= (HBITMAP)::GetCurrentObject(hDC,OBJ_BITMAP);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        ::GetObject(hBitmap,
sizeof(BITMAP),&Bitmap); 
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
if(Bitmap.bmHeight <= 0||Bitmap.bmWidth <= 0||Bitmap.bmWidthBytes <= 0)
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            
return FALSE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        BITMAPINFOHEADER
& bih = m_bmi.bmiHeader;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        ::ZeroMemory( 
&bih, sizeof( BITMAPINFOHEADER ));
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bih.biSize        
= sizeof( BITMAPINFOHEADER );
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bih.biWidth       
= Bitmap.bmWidth;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bih.biHeight      
= Bitmap.bmHeight;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bih.biCompression 
= BI_RGB;//BI_JPEG;//BI_JPEG
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
        bih.biPlanes      = 1;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bih.biBitCount      
= 24;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
int nLineDataSize = ((bih.biBitCount * Bitmap.bmWidth+31)/32)*4;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bih.biWidth       
= nLineDataSize/(bih.biBitCount/8);//(width * nChannels * 8 +31) / 8;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
        int nLineCopySize = bih.biWidth*(bih.biBitCount/8);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        DWORD dwWholeSize 
= nLineDataSize * abs(bih.biHeight);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        BYTE
* pTemp = new BYTE[dwWholeSize];
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        memset(pTemp,
0,dwWholeSize);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
int nLine = ::GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight,
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            pTemp,
&m_bmi, DIB_RGB_COLORS);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
if(nLine <= 0)
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第35张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第36张        
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            delete [] pTemp;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            
return FALSE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第40张        }

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        BOOL bSuccess
=CreateBitmapIndirect(&m_bmi, pTemp);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        delete [] pTemp;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
return bSuccess;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第44张    }

然后在外部如此调用

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        CDC * pScreenDC = new CDC;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        pScreenDC
->CreateCompatibleDC(pDC);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        CBitmap TempBitmap;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        TempBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        CBitmap
* pOldScreenDC = NULL;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        pOldScreenDC 
= (CBitmap*)pScreenDC->SelectObject(&TempBitmap);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        pScreenDC->StretchBlt(0,0,rect.Width(),rect.Height(),pMemDC,0,0,BIT.bmWidth,BIT.bmHeight,SRCCOPY);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        DrawTxtInDC(pScreenDC,
5,15,topinfo,strPosition,rect.Height());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        
if(g_ScreenBitmap.LoadFromHDC(pScreenDC->GetSafeHdc()))
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第2张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第3张        
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            m_bHaveDataInBuffer 
= TRUE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第2张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第3张        }
else使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            m_bHaveDataInBuffer 
= FALSE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第44张        }

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        pScreenDC
->SelectObject(pOldScreenDC);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        delete pScreenDC;

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

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张    //不支持调色板
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张
    BOOL lg_Bitmap::SaveThis(LPCTSTR fn)
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第2张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第3张    
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        BITMAP bmp;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        GetBitmap(
&bmp);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
return Save(&m_bmi.bmiHeader,bmp.bmBits,fn);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第44张    }

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张    BOOL lg_Bitmap::Save(LPBITMAPINFOHEADER lpIn,LPVOID lpBuf,LPCTSTR fn)
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第2张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第3张    
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
//位图文件大小 , 写入文件字节数
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
        DWORD dwBmBitsSize,dwDIBSize;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
//位图文件头结构
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
        BITMAPFILEHEADER   bmfHdr;    
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张    
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
//指向位图信息头结构
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
        LPBITMAPINFOHEADER lpbi;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        dwBmBitsSize 
= ((lpIn->biWidth *lpIn->biBitCount+31)/32)* 4*lpIn->biHeight;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
//为位图内容分配内存
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
        HANDLE hDib  = GlobalAlloc(GHND,dwBmBitsSize+sizeof(BITMAPINFOHEADER));
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        lpbi 
= (LPBITMAPINFOHEADER)GlobalLock(hDib);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        memcpy(lpbi,lpIn,
sizeof(BITMAPINFOHEADER));
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        memcpy((LPSTR)lpbi 
+ sizeof(BITMAPINFOHEADER),lpBuf,dwBmBitsSize);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
// 设置位图文件头
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
        bmfHdr.bfType = 0x4D42;  // "BM"
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
        dwDIBSize    = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;  
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bmfHdr.bfSize 
= dwDIBSize;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bmfHdr.bfReserved1 
= 0;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bmfHdr.bfReserved2 
= 0;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        bmfHdr.bfOffBits 
= (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
if(!SaveDataToFile((LPSTR)&bmfHdr,sizeof(BITMAPFILEHEADER),fn,TRUE))
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第35张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第36张        
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            GlobalUnlock(hDib);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            GlobalFree(hDib);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            
return FALSE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第40张        }

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
if(!SaveDataToFile((LPSTR)lpbi,dwDIBSize,fn))
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第35张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第36张        
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            GlobalUnlock(hDib);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            GlobalFree(hDib);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            
return FALSE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第40张        }

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        GlobalUnlock(hDib);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        GlobalFree(hDib);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张        
return TRUE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第44张    }

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

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

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

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        CDC * pScreenDC = new CDC;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        pScreenDC
->CreateCompatibleDC(pDC);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        CBitmap TempBitmap;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        TempBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        CBitmap
* pOldScreenDC = NULL;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        pOldScreenDC 
= (CBitmap*)pScreenDC->SelectObject(&TempBitmap);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        pScreenDC
->SetStretchBltMode(COLORONCOLOR);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        pScreenDC
->StretchBlt(0,0,rect.Width(),rect.Height(),pMemDC,0,0,BIT.bmWidth,BIT.bmHeight,SRCCOPY);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        DrawTxtInDC(pScreenDC,
5,15,topinfo,strPosition,rect.Height());
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        
if(g_ScreenBitmap.LoadFromHDC(pScreenDC->GetSafeHdc()))
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第2张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第3张        
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            m_bHaveDataInBuffer 
= TRUE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第2张使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第3张        }
else使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第4张{
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第5张            m_bHaveDataInBuffer 
= FALSE;
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第44张        }

使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        pScreenDC
->SelectObject(pOldScreenDC);
使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)第1张        delete pScreenDC;

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

免责声明:文章转载自《使用StretchBlt之前一定要用SetStretchBltMode(COLORONCOLOR)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇BW一些关于DTP的一些链接Jsoup+FastJson制作新闻数据接口-Demo下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用GDI+保存位图文件为PNG文件

1.添加头文件 #include <ocidl.h>  #include <olectl.h>#include <stdio.h>#include <atlconv.h> #include <GdiPlus.h>using namespace Gdiplus; #pragma comment( l...

关于Android中的三级缓存

三级缓存的提出就是为了提升用户体验。当我们第一次打开应用获取图片时,先到网络去下载图片,然后依次存入内存缓存,磁盘缓存,当我们再一次需要用到刚才下载的这张图片时,就不需要再重复的到网络上去下载,直接可以从内存缓存和磁盘缓存中找,由于内存缓存速度较快,我们优先到内存缓存中寻找该图片,如果找到则运用,如果没有找到(内存缓存大小有限),那么我们再到磁盘缓存中去找...

Winform 自定义文本框

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading....

一个具体的列子:vc操作ftp

一个ftp操作的例子: /**//*9/**//*46/**//*86/**//*112/**//*151{156::EnterCriticalSection(&m_rCriticalSection);157if(m_pInetSession==NULL)158m_pInetSession=newCInternetSession(AfxGetAp...

C# 实现预览dwg文件完整源代码(无需autocad环境)

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; namespace WindowsApplica...

lua内存管理

本文内容基于版本:Lua5.3.0 Lua内存管理器规则 Lua允许用户自定义内存管理器,并在创建Lua虚拟机(lua_State实例)时传入。当然自定义内存管理器必须遵循Lua已定义的一些行为规则。创建一个Lua虚拟机需要使用luaL_newstate函数: lua_State *L = luaL_newstate(); luaL_newstate函数的...