




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
第C++實現(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鍵
按下之后,截取整個屏幕的畫面到剪切板里。可以復制到其他軟件里,比如系統(tǒng)的畫圖工具,OfficeWord等。
(2)Alt+PrintScreen組合鍵
按下之后,截取當前活動窗口的畫面到剪切板里。
1.2win10自帶截圖(Win+Shift+S)
按下該組合鍵之后,使用鼠標在屏幕上畫出想要截取的矩形區(qū)域,自動保存到系統(tǒng)剪切板里。
1.3系統(tǒng)自帶的截圖小工具
1.4ffmpeg
ffmpeg-i“輸入視頻”-fflagsnobuffer-t60-ss0“輸出地址”
說明:代表截取輸入視頻從0秒到60秒的片段,保存到輸出地址。
-ssn:起始時間為第n秒
-tn:總共截取的片段時長為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)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 項目資金合同協(xié)議書
- 合同和協(xié)議書意思
- 租山頭合同協(xié)議書
- 怎么寫好合同協(xié)議書
- 合同協(xié)議書重要嗎
- 加工產(chǎn)品合同協(xié)議書
- 配送供貨合同協(xié)議書
- 終止咨詢合同協(xié)議書
- 租臨合同協(xié)議書
- 車子合同協(xié)議書模板
- 外國畫家作品介紹賞析
- 巖土工程勘察報告
- 中藥養(yǎng)護記錄表
- 哈弗H5汽車說明書
- 音樂鑒賞(西安交通大學)知到章節(jié)答案智慧樹2023年
- 2023年成都市新都區(qū)九年級二診英語試題(含答案和音頻)
- 金屬與石材幕墻工程技術(shù)規(guī)范-JGJ133-2013含條文說
- 分包合法合規(guī)宣貫(2017年6月)
- GB 18613-2020電動機能效限定值及能效等級
- 《行政組織學結(jié)課論文綜述3000字》
- 2023年浙江省高中數(shù)學學業(yè)水平考試知識條目精校版
評論
0/150
提交評論