




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第C++實(shí)現(xiàn)截圖截屏的示例代碼目錄1、截圖工具1.1鍵盤截圖(PrtScn鍵)1.2win10自帶截圖(Win+Shift+S)1.3系統(tǒng)自帶的截圖小工具1.4ffmpeg1.5ScreenToGif1.6Chrome2、C++、GDI2.1微軟官方例子2.2C++、GDI、CImage3、C++、OpenGL4、C++、OpenCV5、C++、QT
1、截圖工具
1.1鍵盤截圖(PrtScn鍵)
如何使用MicrosoftWindows操作系統(tǒng)中的PrintScreen(打印屏幕)鍵
(1)PrintScreen鍵
按下之后,截取整個(gè)屏幕的畫面到剪切板里。可以復(fù)制到其他軟件里,比如系統(tǒng)的畫圖工具,OfficeWord等。
(2)Alt+PrintScreen組合鍵
按下之后,截取當(dāng)前活動窗口的畫面到剪切板里。
1.2win10自帶截圖(Win+Shift+S)
按下該組合鍵之后,使用鼠標(biāo)在屏幕上畫出想要截取的矩形區(qū)域,自動保存到系統(tǒng)剪切板里。
1.3系統(tǒng)自帶的截圖小工具
1.4ffmpeg
ffmpeg-i“輸入視頻”-fflagsnobuffer-t60-ss0“輸出地址”
說明:代表截取輸入視頻從0秒到60秒的片段,保存到輸出地址。
-ssn:起始時(shí)間為第n秒
-tn:總共截取的片段時(shí)長為n秒
運(yùn)行后會生成截圖:out1.jpgout2.jpgout3.jpg…
ffmpeg-ifight.mp4-r1-t200-ss1-fimage2out%d.jpg
1.5ScreenToGif
1.6Chrome
2、C++、GDI
2.1微軟官方例子
/en-us/windows/win32/gdi/capturing-an-image
intCaptureAnImage(HWNDhWnd)
HDChdcScreen;
HDChdcWindow;
HDChdcMemDC=NULL;
HBITMAPhbmScreen=NULL;
BITMAPbmpScreen;
DWORDdwBytesWritten=0;
DWORDdwSizeofDIB=0;
HANDLEhFile=NULL;
char*lpbitmap=NULL;
HANDLEhDIB=NULL;
DWORDdwBmpSize=0;
//Retrievethehandletoadisplaydevicecontextfortheclient
//areaofthewindow.
hdcScreen=GetDC(NULL);
hdcWindow=GetDC(hWnd);
//CreateacompatibleDC,whichisusedinaBitBltfromthewindowDC.
hdcMemDC=CreateCompatibleDC(hdcWindow);
if(!hdcMemDC)
MessageBox(hWnd,L"CreateCompatibleDChasfailed",L"Failed",MB_OK);
gotodone;
//Gettheclientareaforsizecalculation.
RECTrcClient;
GetClientRect(hWnd,rcClient);
//Thisisthebeststretchmode.
SetStretchBltMode(hdcWindow,HALFTONE);
//ThesourceDCistheentirescreen,andthedestinationDCisthecurrentwindow(HWND).
if(!StretchBlt(hdcWindow,
0,0,
rcClient.right,rcClient.bottom,
hdcScreen,
0,0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
SRCCOPY))
MessageBox(hWnd,L"StretchBlthasfailed",L"Failed",MB_OK);
gotodone;
//CreateacompatiblebitmapfromtheWindowDC.
hbmScreen=CreateCompatibleBitmap(hdcWindow,rcClient.right-rcClient.left,rcClient.bottom-rcClient.top);
if(!hbmScreen)
MessageBox(hWnd,L"CreateCompatibleBitmapFailed",L"Failed",MB_OK);
gotodone;
//SelectthecompatiblebitmapintothecompatiblememoryDC.
SelectObject(hdcMemDC,hbmScreen);
//BitblocktransferintoourcompatiblememoryDC.
if(!BitBlt(hdcMemDC,
0,0,
rcClient.right-rcClient.left,rcClient.bottom-rcClient.top,
hdcWindow,
0,0,
SRCCOPY))
MessageBox(hWnd,L"BitBlthasfailed",L"Failed",MB_OK);
gotodone;
//GettheBITMAPfromtheHBITMAP.
GetObject(hbmScreen,sizeof(BITMAP),bmpScreen);
BITMAPFILEHEADERbmfHeader;
BITMAPINFOHEADERbi;
bi.biSize=sizeof(BITMAPINFOHEADER);
bi.biWidth=bmpScreen.bmWidth;
bi.biHeight=bmpScreen.bmHeight;
bi.biPlanes=1;
bi.biBitCount=32;
bi.biCompression=BI_RGB;
bi.biSizeImage=0;
bi.biXPelsPerMeter=0;
bi.biYPelsPerMeter=0;
bi.biClrUsed=0;
bi.biClrImportant=0;
dwBmpSize=((bmpScreen.bmWidth*bi.biBitCount+31)/32)*4*bmpScreen.bmHeight;
//Startingwith32-bitWindows,GlobalAllocandLocalAllocareimplementedaswrapperfunctionsthat
//callHeapAllocusingahandletotheprocess'sdefaultheap.Therefore,GlobalAllocandLocalAlloc
//havegreateroverheadthanHeapAlloc.
hDIB=GlobalAlloc(GHND,dwBmpSize);
lpbitmap=(char*)GlobalLock(hDIB);
//Getsthe"bits"fromthebitmap,andcopiesthemintoabuffer
//that'spointedtobylpbitmap.
GetDIBits(hdcWindow,hbmScreen,0,
(UINT)bmpScreen.bmHeight,
lpbitmap,
(BITMAPINFO*)bi,DIB_RGB_COLORS);
//Afileiscreated,thisiswherewewillsavethescreencapture.
hFile=CreateFile(L"captureqwsx.bmp",
GENERIC_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,NULL);
//Addthesizeoftheheaderstothesizeofthebitmaptogetthetotalfilesize.
dwSizeofDIB=dwBmpSize+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
//Offsettowheretheactualbitmapbitsstart.
bmfHeader.bfOffBits=(DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER);
//Sizeofthefile.
bmfHeader.bfSize=dwSizeofDIB;
//bfTypemustalwaysbeBMforBitmaps.
bmfHeader.bfType=0x4D42;//BM.
WriteFile(hFile,(LPSTR)bmfHeader,sizeof(BITMAPFILEHEADER),dwBytesWritten,NULL);
WriteFile(hFile,(LPSTR)bi,sizeof(BITMAPINFOHEADER),dwBytesWritten,NULL);
WriteFile(hFile,(LPSTR)lpbitmap,dwBmpSize,dwBytesWritten,NULL);
//UnlockandFreetheDIBfromtheheap.
GlobalUnlock(hDIB);
GlobalFree(hDIB);
//Closethehandleforthefilethatwascreated.
CloseHandle(hFile);
//Cleanup.
done:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL,hdcScreen);
ReleaseDC(hWnd,hdcWindow);
return0;
}
2.2C++、GDI、CImage
HDChdcSrc=GetDC(NULL);
intnBitPerPixel=GetDeviceCaps(hdcSrc,BITSPIXEL);
intnWidth=GetDeviceCaps(hdcSrc,HORZRES);
intnHeight=GetDeviceCaps(hdcSrc,VERTRES);
CImageimage;
image.Create(nWidth,nHeight,nBitPerPixel);
BitBlt(image.GetDC(),0,0,nWidth,nHeight,hdcSrc,0,0,SRCCOPY);
ReleaseDC(NULL,hdcSrc);
image.ReleaseDC();
image.Save(s,Gdiplus::ImageFormatPNG);
3、C++、OpenGL
voidCaptureOpenGLWindow(constchar*savePath,intw,inth)
GLubyte*pPixelData;
GLintPixelDataLength;
//分配內(nèi)存和打開文件
pPixelData=(GLubyte*)malloc(w*h*3);
if(pPixelData==0)
return;
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,pPixelData);
stbi_write_png(savePath,w,h,3,pPixelData,0);
free(pPixelData);
intiw=w,ih=h,n=3;
stbi_set_flip_vertically_on_load(true);
unsignedchar*idata=stbi_load(savePath,iw,ih,n,0);
stbi_write_png(savePath,w,h,3,idata,0);
stbi_image_free(idata);
4、C++、OpenCV
BITMAPINFOHEADERcreateBitmapHeader(intwidth,intheight)
BITMAPINFOHEADERbi;
//createabitmap
bi.biSize=sizeof(BITMAPINFOHEADER);
bi.biWidth=width;
bi.biHeight=-height;//thisisthelinethatmakesitdrawupsidedownornot
bi.biPlanes=1;
bi.biBitCount=32;
bi.biCompression=BI_RGB;
bi.biSizeImage=0;
bi.biXPelsPerMeter=0;
bi.biYPelsPerMeter=0;
bi.biClrUsed=0;
bi.biClrImportant=0;
returnbi;
MatcaptureScreenMat(HWNDhwnd)
Matsrc;
//gethandlestoadevicecontext(DC)
HDChwindowDC=GetDC(hwnd);
HDChwindowCompatibleDC=CreateCompatibleDC(hwindowDC);
SetStretchBltMode(hwindowCompatibleDC,COLORONCOLOR);
//definescale,heightandwidth
intscreenx=GetSystemMetrics(SM_XVIRTUALSCREEN);
intscreeny=GetSystemMetrics(SM_YVIRTUALSCREEN);
intwidth=GetSystemMetrics(SM_CXVIRTUALSCREEN);
intheight=GetSystemMetrics(SM_CYVIRTUALSCREEN);
//creatematobject
src.create(height,width,CV_8UC4);
//createabitmap
HBITMAPhbwindow=CreateCompatibleBitmap(hwindowDC,width,height);
BITMAPINFOHEADERbi=createBitmapHeader(width,height);
//usethepreviouslycreateddevicecontextwiththebitmap
SelectObject(hwindowCompatibleDC,hbwindow);
//copyfromthewindowdevicecontexttothebitmapdevicecontext
S
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 國網(wǎng)繼電保護(hù)技術(shù)培訓(xùn)體系
- 小學(xué)生語文寫作培訓(xùn)課件
- 城市交通規(guī)劃合同管理合同管理咨詢重點(diǎn)基礎(chǔ)知識點(diǎn)
- 我的童年音樂課件
- 試驗(yàn)檢測單位安全培訓(xùn)課件
- 《當(dāng)代少先隊(duì)教育導(dǎo)論》課件-【第8章】 少先隊(duì)儀式教育
- 跟單文員合同協(xié)議范本
- 浮苔打撈協(xié)議書
- 超市租賃協(xié)議合同協(xié)議
- 車合同補(bǔ)充協(xié)議模板
- 智能界面布局研究-全面剖析
- 課題申報(bào)書:數(shù)智融合驅(qū)動高校教師數(shù)字素養(yǎng)提升路徑研究
- 2025年北京市房山區(qū)九年級初三一模物理試卷(含答案)
- 外賣配送員工作流程總結(jié)
- 新式茶飲產(chǎn)業(yè)的技術(shù)發(fā)展現(xiàn)狀與未來創(chuàng)新趨勢
- 【國浩律師事務(wù)所】2025中國企業(yè)出海戰(zhàn)略與法律支持需求調(diào)研報(bào)告
- 2025中國低空經(jīng)濟(jì)城市發(fā)展指數(shù)報(bào)告
- 哈爾濱中考英語單選題型100道及答案
- 湖南省長沙市岳麓區(qū)湖南師范大學(xué)附中2025屆高三下學(xué)期第六次檢測化學(xué)試卷含解析
- 2024-2025學(xué)年新教材高中生物 第五章 生物的進(jìn)化 第二節(jié) 適應(yīng)是自然選擇的結(jié)果教學(xué)設(shè)計(jì)(2)浙科版必修2
- 蘭州2025年中國農(nóng)業(yè)科學(xué)院蘭州畜牧與獸藥研究所招聘16人筆試歷年參考題庫附帶答案詳解
評論
0/150
提交評論