保存对话框的截图错位了,不知道哪里错了,向大虾求教正常
错位
代码如下:
void CTestDlg::savePic(CString picName)
{
CDC dc;
dc.CreateDC((LPCTSTR)"DISPLAY",NULL,NULL,NULL); CDC memDC ;
CBitmap memBitmap ;
CBitmap* oldBitmap ; CRect rect;
GetWindowRect(&rect);
//ScreenToClient(&rect);
int m_nClientWidth = rect.Width();//
int m_nClientHeight = rect.Height();// memDC.CreateCompatibleDC(&dc) ;
memBitmap.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap) ; if (memDC.GetSafeHdc() != NULL)
{
memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,&dc, rect.left, rect.top, SRCCOPY); memDC.SelectObject(oldBitmap);
} BITMAP btm; //获取bmp文件信息
memBitmap.GetBitmap(&btm); //分配内存
DWORD size=btm.bmWidthBytes*btm.bmHeight;
LPSTR lpData=(LPSTR)GlobalAlloc(GPTR,size);
if(lpData != NULL)
{
//填充bmp头文件信息
BITMAPINFOHEADER bih;
bih.biBitCount=btm.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=btm.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=btm.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
GetDIBits(dc.m_hDC,memBitmap,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS); //从bm中获取bmp数据 BITMAPFILEHEADER bfh; //bmp文件头定义
bfh.bfReserved1=bfh.bfReserved2=0;
bfh.bfType=((WORD)('M'<<8)|'B');
bfh.bfSize=sizeof(bfh)+size;
bfh.bfOffBits=sizeof(bfh); CFile bf;
if(bf.Open(picName,CFile::modeCreate|CFile::modeWrite))
{
bf.Write(&bfh,sizeof(BITMAPFILEHEADER));
bf.Write(&bih,sizeof(BITMAPINFOHEADER));
bf.Write(lpData,size);
bf.Close();
}
::GlobalFree(lpData);
}
dc.DeleteDC();
}
错位
代码如下:
void CTestDlg::savePic(CString picName)
{
CDC dc;
dc.CreateDC((LPCTSTR)"DISPLAY",NULL,NULL,NULL); CDC memDC ;
CBitmap memBitmap ;
CBitmap* oldBitmap ; CRect rect;
GetWindowRect(&rect);
//ScreenToClient(&rect);
int m_nClientWidth = rect.Width();//
int m_nClientHeight = rect.Height();// memDC.CreateCompatibleDC(&dc) ;
memBitmap.CreateCompatibleBitmap(&dc, m_nClientWidth, m_nClientHeight) ;
oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap) ; if (memDC.GetSafeHdc() != NULL)
{
memDC.BitBlt(0, 0, m_nClientWidth, m_nClientHeight,&dc, rect.left, rect.top, SRCCOPY); memDC.SelectObject(oldBitmap);
} BITMAP btm; //获取bmp文件信息
memBitmap.GetBitmap(&btm); //分配内存
DWORD size=btm.bmWidthBytes*btm.bmHeight;
LPSTR lpData=(LPSTR)GlobalAlloc(GPTR,size);
if(lpData != NULL)
{
//填充bmp头文件信息
BITMAPINFOHEADER bih;
bih.biBitCount=btm.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=btm.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=btm.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
GetDIBits(dc.m_hDC,memBitmap,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS); //从bm中获取bmp数据 BITMAPFILEHEADER bfh; //bmp文件头定义
bfh.bfReserved1=bfh.bfReserved2=0;
bfh.bfType=((WORD)('M'<<8)|'B');
bfh.bfSize=sizeof(bfh)+size;
bfh.bfOffBits=sizeof(bfh); CFile bf;
if(bf.Open(picName,CFile::modeCreate|CFile::modeWrite))
{
bf.Write(&bfh,sizeof(BITMAPFILEHEADER));
bf.Write(&bih,sizeof(BITMAPINFOHEADER));
bf.Write(lpData,size);
bf.Close();
}
::GlobalFree(lpData);
}
dc.DeleteDC();
}
解决方案 »
- VC怎么去掉滚动条
- vector<int> m_vector,其中一个线程将第一个元素erase掉,另一个线程往m_vector push_back值,那么这样会不会出现问题?如下
- 如何传递CArray对象呢?请高手指教
- ISAPI连接SQL数据库问题
- 【测试驱动开发】寻找C/C++环境下的单元测试工具
- 第一个windows程序失败!求救~~
- 怎样使模式对话框最大化显示?
- 请问有谁是编程沙龙的会员?帮我下载一个文件给高分谢谢!!
- 程序中使用SMTP协议发送邮件失败。
- 在MDI中给树控件添加一个item时遇到的问题
- 请人帮忙完成一个功能模块--关于可插入协议的Mime Filter(给报酬)
- 请教下简单iocp代码的一个问题
我新建一个MFC对话框,用按钮启动的这段代码
如果不是,换成4的倍数试试