首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

截取桌面并且BMP文件保存,出现了一点有关问题

2012-11-03 
截取桌面并且BMP文件保存,出现了一点问题下面是代码:不知道哪里出了问题,请大家帮我找找C/C++ code//定义

截取桌面并且BMP文件保存,出现了一点问题
下面是代码:
不知道哪里出了问题,请大家帮我找找

C/C++ code
    //定义图形大小    CRect re;    GetDesktopWindow()->GetWindowRect(&re);    int iWidth = re.Width() ;     int iHeight = re.Height() ;    int iPixel  = 24;    //图形格式参数    LPBITMAPINFO lpbmih = new BITMAPINFO;    lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);    lpbmih->bmiHeader.biWidth = iWidth;    lpbmih->bmiHeader.biHeight = iHeight;    lpbmih->bmiHeader.biPlanes = 1;    lpbmih->bmiHeader.biBitCount = iPixel;    lpbmih->bmiHeader.biCompression = BI_RGB;    lpbmih->bmiHeader.biSizeImage = 0;    lpbmih->bmiHeader.biXPelsPerMeter = 0;    lpbmih->bmiHeader.biYPelsPerMeter = 0;    lpbmih->bmiHeader.biClrUsed = 0;    lpbmih->bmiHeader.biClrImportant = 0;    //创建位图数据    HDC hdc,hdcMem;    HBITMAP hBitMap = NULL;    CBitmap *pBitMap = NULL;    CDC *pMemDC = NULL;    BYTE *pBits;  //  hdc = CreateIC(TEXT("DISPLAY"),NULL,NULL,NULL);    hdc = GetDesktopWindow()->GetWindowDC()->m_hDC ;    hdcMem = CreateCompatibleDC(hdc);    hBitMap = CreateDIBSection(hdcMem,lpbmih,DIB_PAL_COLORS,(void **)&pBits,NULL,0);    pBitMap = new CBitmap;    pBitMap->Attach(hBitMap);    pMemDC = new CDC;    pMemDC->Attach(hdcMem);    pMemDC->SelectObject(pBitMap);     //    CRect rc(0,0,iWidth,iHeight);    pMemDC->SetBkMode(TRANSPARENT);    //添加自绘图形     DrawCurve(pMemDC,rc);    //保存到文件并创建位图结构    BITMAPFILEHEADER bmfh;    ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER));    *((char *)&bmfh.bfType) = 'B';    *(((char *)&bmfh.bfType) + 1) = 'M';    bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);    bmfh.bfSize = bmfh.bfOffBits + (iWidth * iHeight) * iPixel / 8;    TCHAR szBMPFileName[128];    int iBMPBytes = iWidth * iHeight * iPixel / 8;    strcpy(szBMPFileName,filename);    CFile file;    if(file.Open(szBMPFileName,CFile::modeWrite | CFile::modeCreate))    {        file.Write(&bmfh,sizeof(BITMAPFILEHEADER));        file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER));        file.Write(pBits,iBMPBytes);        file.Close();    }    pMemDC->DeleteDC();    delete pMemDC;    pMemDC  = NULL;    delete pBitMap; pBitMap = NULL;    delete lpbmih;  lpbmih  = NULL;


[解决办法]
以下供参考
BOOL CLLK::LLKScreenShot()
{
strcpy ( this->szWindowTitle, "连连看") ;

// 寻找LLK窗口
this->m_hWnd = FindWindowA ( NULL, szWindowTitle ) ;
if ( m_hWnd == NULL )
return FALSE;

// 设置客户区域坐标
RECT WindowRect ;
GetWindowRect ( this->m_hWnd, &WindowRect ) ;
this->m_dwClientPosX = WindowRect.left ;
this->m_dwClientPosY = WindowRect.top ;

// 创建屏幕DC
this->m_screenDC.DeleteDC () ;
this->m_screenDC.CreateDC ( L"DISPLAY", NULL, NULL, NULL ) ;

DWORD x = (m_dwIconSizeX + m_byteIconMarginX)*m_dwIconNumX;
DWORD y = (m_dwIconSizeY + m_byteIconMarginY)*m_dwIconNumY;
DWORD srcX = this->m_dwClientPosX + m_dwStartOffsetX;
DWORD srcY = this->m_dwClientPosY + m_dwStartOffsetY;

m_bmpIcons.DeleteObject();
CDCMemoryDC ;
m_bmpIcons.CreateCompatibleBitmap ( &this->m_screenDC, x, y) ;
MemoryDC.CreateCompatibleDC ( &this->m_screenDC ) ;
MemoryDC.SelectObject ( &m_bmpIcons ) ;
MemoryDC.BitBlt ( 0, 0, x, y, &this->m_screenDC, srcX, srcY, SRCCOPY );
MemoryDC.DeleteDC();

return TRUE;
}
[解决办法]
C/C++ code
void CapScreen(char filename[]){    CDC *pDC;    pDC = CDC::FromHandle(GetDC(GetDesktopWindow()));    if(pDC == NULL) return;    int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);    int Width = pDC->GetDeviceCaps(HORZRES);    int Height = pDC->GetDeviceCaps(VERTRES);        CDC memDC;    if(memDC.CreateCompatibleDC(pDC) == 0) return;        CBitmap memBitmap, *oldmemBitmap;    if(memBitmap.CreateCompatibleBitmap(pDC, Width, Height) == NULL) return;        oldmemBitmap = memDC.SelectObject(&memBitmap);    if(oldmemBitmap == NULL) return;    if(memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY) == 0) return;        BITMAP bmp;    memBitmap.GetBitmap(&bmp);        FILE *fp = fopen(filename, "w+b");        BITMAPINFOHEADER bih = {0};    bih.biBitCount = bmp.bmBitsPixel;    bih.biCompression = BI_RGB;    bih.biHeight = bmp.bmHeight;    bih.biPlanes = 1;    bih.biSize = sizeof(BITMAPINFOHEADER);    bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;    bih.biWidth = bmp.bmWidth;        BITMAPFILEHEADER bfh = {0};    bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);    bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;    bfh.bfType = (WORD)0x4d42;        fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);        fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);        byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];        GetDIBits(memDC.m_hDC,        (HBITMAP) memBitmap.m_hObject,        0,        Height,        p,        (LPBITMAPINFO) &bih,        DIB_RGB_COLORS);        fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);        delete [] p;        fclose(fp);        memDC.SelectObject(oldmemBitmap);} 


[解决办法]
pMemDC = new CDC;
pMemDC->Attach(hdcMem);
#ifndef TEST
CBitmap *pbmpx=pMemDC->GetCurrentBitmap();
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP,pbmpx->GetSafeHandle());
CloseClipboard();
#endif

热点排行