hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL)
2 用这个API 来为屏幕设备描述表创建兼容的内存设备描述表
WINGDIAPI HBITMAP WINAPI CreateCompatibleBitmap( __in HDC hdc, __in int cx, __in int cy)
3 获得选定区域坐标
可以做一个矩形框用来选择截取的范围将坐标传回
4 创建一个与屏幕设备描述表兼容的位图
hBitmap = CreateCompatibleBitmap(hScrDC, Width, Height)
5 把新位图选到内存设备描述表中
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap)
6 把屏幕设备描述表拷贝到内存设备描述表中
BitBlt(hMemDC, 0, 0, Width, Height,hScrDC, nX, nY, SRCCOPY)
7得到屏幕位图的句柄
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap)
8清空剪贴板,把屏幕内容粘贴到剪贴板上,
EmptyClipboard()
SetClipboardData(CF_BITMAP, hBitmap)
CloseClipboard()
代码 大概就是这样 有几个关键函数仔细一下,要是要完整代码呢留言CDC memDC
CBitmap memBitmap,*pOldBitmap
CWindowDC dc(GetDesktopWindow())
memDC.CreateCompatibleDC(&dc)
int nWidth,nHeight
/* nWidth=GetSystemMetrics(SM_CXSCREEN)
nHeight=GetSystemMetrics(SM_CYSCREEN)*/
CRect rect
GetDesktopWindow()->GetWindowRect(rect)
ClientToScreen(&rect)
nWidth=rect.Width()
nHeight=rect.Height()
memBitmap.CreateCompatibleBitmap(&dc,nWidth,nHeight)
pOldBitmap=memDC.SelectObject(&memBitmap)
// memDC.StretchBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,rect.Width(),rect.Height(),SRCCOPY)
memDC.BitBlt(0,0,nWidth,nHeight,&dc,0,0,SRCCOPY)
BITMAPFILEHEADER BMFhead
BITMAPINFOHEADER BMIhead
BMFhead.bfReserved1=0
BMFhead.bfReserved2=0
BMFhead.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)
BMFhead.bfSize=BMFhead.bfOffBits+nWidth*nHeight*4
BMFhead.bfType=0x4d42
BMIhead.biBitCount=32
BMIhead.biClrImportant=0
BMIhead.biClrUsed=0
BMIhead.biSizeImage=0
BMIhead.biXPelsPerMeter=0
BMIhead.biYPelsPerMeter=0
BMIhead.biCompression=BI_RGB
BMIhead.biHeight=nHeight
BMIhead.biPlanes=1
BMIhead.biSize=sizeof(BITMAPINFOHEADER)
BMIhead.biWidth=nWidth
DWORD dwSize=nWidth*nHeight*4
BYTE *pData=new BYTE[dwSize]
GetDIBits(memDC.m_hDC,(HBITMAP)memBitmap.m_hObject,0,nHeight,pData,(LPBITMAPINFO)&BMIhead,DIB_RGB_COLORS)
CFile file
file.Open("test.bmp",CFile::modeCreate | CFile::modeWrite)
file.Write(&BMFhead,sizeof(BITMAPFILEHEADER))
file.Write(&BMIhead,sizeof(BITMAPINFOHEADER))
file.Write(pData,dwSize)
file.Close()
memDC.SelectObject(pOldBitmap)
MessageBox("绘图完毕")
GetParent()->ShowWindow(SW_SHOW)
mfc实现截图文字识别步骤如下1、点击“打开图片”,可对bmp格式图片进行文字识别
2、点击“截取图片”,鼠标变成十字,按下鼠标左键开始拖拽截取区域,松开鼠标左键完成截取图片并进行文字识别。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)