VC++ 关于截取图象的操作问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 08:00:15
HBITMAP GetDlgBmp()
{
HDC hsrc=::GetDC(hwnd);//获取对话框的DC
HDC hmemdc=::CreateCompatibleDC(hsrc);
RECT rc;
::GetWindowRect(hwnd,&rc);
SIZE sz;
sz.cx=rc.right-rc.left;
sz.cy=rc.bottom-rc.top;
HBITMAP hbmp=::CreateCompatibleBitmap(hsrc,sz.cx.sz.cy);
HGDIOBJ holdbmp=::SelectObject(hmemdc,hbmp);
::BitBlt(hmemdc,0,0,sz.cx.sz.cy,hsrc,rc.left,rc.top,SRCCOPY);
::SelectObject(hmemdc,holdbmp);
::DeleteObject(hmemdc);
::ReleaseDC(hwnd,hsrc);
return hbmp;
}
上网找的代码,这样就获取了一个hbmp句柄...不过这个拿来有什么用呢?
比如如果获取这个截下图的大小,然后存入数据库??
或者不用数据库..就直接保存为bmp文件呢??
能给代码么?或者方法也行!对图象这个操作不熟悉啊!
MSDN看了半天也不知道个所以然啊!!

看看这个吧:

void Screen(char filename[])
{
CDC *pDC;//屏幕DC
pDC = CDC::FromHandle(GetDC(NULL));//获取当前整个屏幕DC
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES);

cout << "当前屏幕色彩模式为" << BitPerPixel << "位色彩" << endl
<< "屏幕宽度:" << Width << endl
<< "屏幕高度:" << Height << endl << endl;

CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);

CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);

oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC

//以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息

FILE *fp = fopen(filename,