我的程序是這樣的,獲取桌面圖像,傳到內存DC,在內存DC中取出圖像,生成紋理(或其他處理操作,比如融合),進行紋理貼圖。
我開始的時候寫了兩個小程序,一個是獲取桌面圖像並顯示,已經成功;一個是從桌面的絕對路徑載入一幅位圖,並用於紋理貼圖,也成功了。
現在想把兩個程序合起來,不用從硬盤讀取的位圖貼圖,改用從DC獲取的位圖貼圖。但是程序運行效果就是貼出的圖灰蒙蒙的白色。經過我自己的實驗,發現如果放位圖的指針地址裡沒有東西的話,就會出現這種效果。
說明我根本沒從內存DC中將位圖取出來,大神們快看看,這個改怎麼解決,已經三天了,實在沒招了。
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT );
GLfloat ctrlpoints[4][4][3] = {
{{-2.5,-3, 0.0},{-2,-1.5,0.0},
{2,-1.5,0.0},{2.5,-3,0.0}},
{{-3,-1.5,0.0},{-2,-0.3,0.0},
{2,-0.3,0.0},{3,-1.5,0.0}},
{{-3.5,0.0,0.0},{-2,0.9,0.0},
{2,0.9,0.0},{3.5,0.0,0.0}},
{{-4,1.5,0.0},{-2,2.1,0.0},
{2,2.1,0.0},{4,1.5,0.0}}
};
GLfloat texpts[2][2][2] = {{{0.0, 0.0}, {1.0, 0.0}},
{{0.0, 1.0}, {1.0, 1.0}}};
glMap2f(GL_MAP2_VERTEX_3,0,1,3,4,0,1,12,4,&ctrlpoints[0][0][0]);
glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2,
0, 1, 4, 2, &texpts[0][0][0]);
glEnable(GL_MAP2_TEXTURE_COORD_2);
glEnable(GL_MAP2_VERTEX_3);
glMapGrid2f(20,0.0,1.0,20,0.0,1.0);
glColor3f (0.0f, 0.0f, 0.0f);
//makeImage();
RECT rcView;
GetClientRect(&rcView);
CDC *deskDC = CDC::FromHandle(::GetDC(NULL));//獲取當前DC
CDC memDC;
memDC.CreateCompatibleDC(deskDC);//創建兼容DC
CBitmap pDeskBmp ;
CBitmap *pOldBmp;
UINT uDeskBmpWidth = ::GetSystemMetrics(SM_CXSCREEN);//屏幕寬度
UINT uDeskBmpHeight = ::GetSystemMetrics(SM_CYSCREEN);//屏幕高度
pDeskBmp.CreateCompatibleBitmap(deskDC, uDeskBmpWidth, uDeskBmpHeight);//創建兼容位圖
pOldBmp = memDC.SelectObject(&pDeskBmp);//將兼容位圖選入創建的DC中
memDC.BitBlt(0, 0, uDeskBmpWidth, uDeskBmpHeight, deskDC, 0, 0, SRCCOPY);//將屏幕圖像復制到內存DC中
BITMAP bmp;
pDeskBmp.GetBitmap(&bmp);
bmp.bmBits=(char*)malloc((bmp.bmWidth)*(bmp.bmHeight)*(bmp.bmBitsPixel));
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0,
// GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, bmp.bmWidth, bmp.bmHeight,
GL_BGR_EXT, GL_UNSIGNED_BYTE, bmp.bmBits);//
glEnable(GL_TEXTURE_2D);
glEvalMesh2(GL_LINE, 0, 20, 0, 20);
glFlush();
問題已經解決,bmp.bmBits=(char*)malloc((bmp.bmWidth)*(bmp.bmHeight)*(bmp.bmBitsPixel));這裡雖然開辟了空間,但是裡面是空的,
////char data = (char)malloc((bmp.bmWidth)*(bmp.bmHeight)*3);
////pOldBmp->GetBitmapBits((bmp.bmWidth)*(bmp.bmHeight)*3, data);
這樣開辟空間就能讀到位圖信息了。