可以参考MSDN上关于DIBAPI和DIBUTIL的内容: HBITMAP DIBToBitmap(HDIB hDIB, HPALETTE hPal) { LPSTR lpDIBHdr, lpDIBBits; // pointer to DIB header, pointer to DIB bits HBITMAP hBitmap; // handle to device-dependent bitmap HDC hDC; // handle to DC HPALETTE hOldPal = NULL; // handle to a palette // if invalid handle, return NULL if (!hDIB) return NULL; // lock memory block and get a pointer to it lpDIBHdr = GlobalLock(hDIB); // get a pointer to the DIB bits lpDIBBits = FindDIBBits(lpDIBHdr); // get a DC hDC = GetDC(NULL); if (!hDC) { // clean up and return NULL GlobalUnlock(hDIB); return NULL; } // select and realize palette if (hPal) hOldPal = SelectPalette(hDC, hPal, FALSE); RealizePalette(hDC); // create bitmap from DIB info. and bits hBitmap = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)lpDIBHdr, CBM_INIT, lpDIBBits, (LPBITMAPINFO)lpDIBHdr, DIB_RGB_COLORS); // restore previous palette if (hOldPal) SelectPalette(hDC, hOldPal, FALSE); // clean up ReleaseDC(NULL, hDC); GlobalUnlock(hDIB); // return handle to the bitmap return hBitmap; }
HDIB CopyScreenToDIB(LPRECT lpRect) { HBITMAP hBitmap; // handle to device-dependent bitmap HPALETTE hPalette; // handle to palette HDIB hDIB = NULL; // handle to DIB
// get the device-dependent bitmap in lpRect by calling // CopyScreenToBitmap and passing it the rectangle to grab
hBitmap = CopyScreenToBitmap(lpRect);
// check for a valid bitmap handle
if (!hBitmap) return NULL;
// get the current palette
hPalette = GetSystemPalette();
// convert the bitmap to a DIB
hDIB = BitmapToDIB(hBitmap, hPalette);
// clean up
DeleteObject(hPalette); DeleteObject(hBitmap);
// return handle to the packed-DIB return hDIB; } HBITMAP CopyScreenToBitmap(LPRECT lpRect) { HDC hScrDC, hMemDC; // screen DC and memory DC HBITMAP hBitmap, hOldBitmap; // handles to deice-dependent bitmaps int nX, nY, nX2, nY2; // coordinates of rectangle to grab int nWidth, nHeight; // DIB width and height int xScrn, yScrn; // screen resolution
// check for an empty rectangle
if (IsRectEmpty(lpRect)) return NULL;
// create a DC for the screen and create // a memory DC compatible to screen DC
http://www.codeproject.com/internet/remotecontrol.asp
燕良 2002年1月
http://www.diamondgarden.net/
像素操作
以上所有操作都局限于标准GDI函数,如果我们要实现更进一步的操作,例如当傍晚你希望把整个画面的颜色渲染能淡红色调,晚上的时候你要把整个画面变暗,早上再把它恢复到原来的亮度这些GDI都无法帮你做到。 如果想达到上述效果,就必须自己对像素的RGB值进行操作。
首先让我们要得到一个Bitmap对象中的像素数据。让我们看一下具体该怎么操作。假设我们有一个mybmp是一个CBitmap对象(或者其派生类对象),下面的代码把CBitmap中的像素取出:
BITMAP bm;
mybmp.GetBitmap(&bm);
BITMAPINFO binfo;
ZeroMemory(&binfo,sizeof(BITMAPINFO));
binfo.bmiHeader.biBitCount=24; //24bit像素格式
binfo.bmiHeader.biCompression=0;
binfo.bmiHeader.biHeight=-bm.bmHeight;
binfo.bmiHeader.biPlanes=1;
binfo.bmiHeader.biSizeImage=0;
binfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
binfo.bmiHeader.biWidth=bm.bmWidth;CClientDC dc(this);
BYTE *pbuf;//用来存储像素数据
int linebytes=(bm.bmWidth*3+3)&(~3);//4字节对齐
int size=linebytes*bm.bmHeight;
pbuf=new BYTE[size];
::GetDIBits(dc,m_bmpSword,0,bm.bmHeight,pbuf,&binfo,DIB_RGB_COLORS);上面代码执行后,我们的pbuf中就存储了从mybmp拷贝而来的像素数据,而且是24bit模式的,这样你就可以对所有这些像素进行你所需要的操作了,例如晚上了,你想把这个Bitmap变暗,我这里粗略的把每个像素的RGB值都降低一半,可以使用下面的循环:
for(int I;I<size;I++)
pbuf[i]=pbuf[i]/2;
得到了像素你就得到了一切,所有操作你都可以进行,例如上面提到的标准GDI不支持的Alpha通道。
呵呵,像素交给你了,这样我就放心了,那我走了…。。等等,你得到了这些像素,但是渲染时我们还是要使用标准GDI操作,所以好把这些像素设置回Bitmap对象中才行,好吧,这其实很简单,继续上面的代码:
SetDIBits(dc,mybmp,0,bm.bmHeight,pbuf,&binfo,DIB_RGB_COLORS);
最后别忘了:
delete[] pbuf;
char *pBuf;CBitmap::GetBitmapBits( nBufKen, pBuf );
CSocket::Send( pBuf, nBufLen );
用jpeg不核算
现在是抓下来得图保存到buf数组里得时候很慢
bitblt操作很慢
祝你好运!
HBITMAP DIBToBitmap(HDIB hDIB, HPALETTE hPal)
{
LPSTR lpDIBHdr, lpDIBBits; // pointer to DIB header, pointer to DIB bits
HBITMAP hBitmap; // handle to device-dependent bitmap
HDC hDC; // handle to DC
HPALETTE hOldPal = NULL; // handle to a palette
// if invalid handle, return NULL
if (!hDIB)
return NULL;
// lock memory block and get a pointer to it
lpDIBHdr = GlobalLock(hDIB);
// get a pointer to the DIB bits
lpDIBBits = FindDIBBits(lpDIBHdr);
// get a DC
hDC = GetDC(NULL);
if (!hDC)
{
// clean up and return NULL
GlobalUnlock(hDIB);
return NULL;
}
// select and realize palette
if (hPal)
hOldPal = SelectPalette(hDC, hPal, FALSE);
RealizePalette(hDC);
// create bitmap from DIB info. and bits
hBitmap = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)lpDIBHdr, CBM_INIT,
lpDIBBits, (LPBITMAPINFO)lpDIBHdr, DIB_RGB_COLORS);
// restore previous palette
if (hOldPal)
SelectPalette(hDC, hOldPal, FALSE);
// clean up
ReleaseDC(NULL, hDC);
GlobalUnlock(hDIB);
// return handle to the bitmap
return hBitmap; }
{
HBITMAP hBitmap; // handle to device-dependent bitmap
HPALETTE hPalette; // handle to palette
HDIB hDIB = NULL; // handle to DIB
// get the device-dependent bitmap in lpRect by calling
// CopyScreenToBitmap and passing it the rectangle to grab
hBitmap = CopyScreenToBitmap(lpRect);
// check for a valid bitmap handle
if (!hBitmap)
return NULL;
// get the current palette
hPalette = GetSystemPalette();
// convert the bitmap to a DIB
hDIB = BitmapToDIB(hBitmap, hPalette);
// clean up
DeleteObject(hPalette);
DeleteObject(hBitmap);
// return handle to the packed-DIB
return hDIB;
} HBITMAP CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC, hMemDC; // screen DC and memory DC
HBITMAP hBitmap, hOldBitmap; // handles to deice-dependent bitmaps
int nX, nY, nX2, nY2; // coordinates of rectangle to grab
int nWidth, nHeight; // DIB width and height
int xScrn, yScrn; // screen resolution
// check for an empty rectangle
if (IsRectEmpty(lpRect))
return NULL;
// create a DC for the screen and create
// a memory DC compatible to screen DC
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC);
// get points of rectangle to grab
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
// get screen resolution
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//make sure bitmap rectangle is visible
if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > xScrn)
nX2 = xScrn;
if (nY2 > yScrn)
nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
// create a bitmap compatible with the screen DC
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
// select new bitmap into memory DC
hOldBitmap = SelectObject(hMemDC, hBitmap);
// bitblt screen DC to memory DC
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
// select old bitmap back into memory DC and get handle to
// bitmap of the screen
hBitmap = SelectObject(hMemDC, hOldBitmap);
// clean up
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// return handle to the bitmap
return hBitmap;
}
保留在内存里的?谢谢大家了
我最近也准备写个类似的程序。
QQ:10992445 不嫌弃可以相互探讨一下