C++實(shí)現(xiàn)截圖截屏的示例代碼_第1頁(yè)
C++實(shí)現(xiàn)截圖截屏的示例代碼_第2頁(yè)
C++實(shí)現(xiàn)截圖截屏的示例代碼_第3頁(yè)
C++實(shí)現(xiàn)截圖截屏的示例代碼_第4頁(yè)
C++實(shí)現(xiàn)截圖截屏的示例代碼_第5頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第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)前活動(dòng)窗口的畫面到剪切板里。

1.2win10自帶截圖(Win+Shift+S)

按下該組合鍵之后,使用鼠標(biāo)在屏幕上畫出想要截取的矩形區(qū)域,自動(dòng)保存到系統(tǒng)剪切板里。

1.3系統(tǒng)自帶的截圖小工具

1.4ffmpeg

ffmpeg-i“輸入視頻”-fflagsnobuffer-t60-ss0“輸出地址”

說(shuō)明:代表截取輸入視頻從0秒到60秒的片段,保存到輸出地址。

-ssn:起始時(shí)間為第n秒

-tn:總共截取的片段時(shí)長(zhǎng)為n秒

運(yùn)行后會(huì)生成截圖: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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論