欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)截圖截屏的示例代碼

 更新時(shí)間:2021年12月06日 10:40:11   作者:愛看書的小沐  
本文主要介紹了C++實(shí)現(xiàn)截圖截屏的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

1、截圖工具

1.1 鍵盤截圖(PrtScn鍵)

如何使用Microsoft Windows操作系統(tǒng)中的Print Screen(打印屏幕)鍵
(1)Print Screen鍵
按下之后,截取整個(gè)屏幕的畫面到剪切板里??梢詮?fù)制到其他軟件里,比如系統(tǒng)的畫圖工具,Office Word等。

(2)Alt+Print Screen組合鍵
按下之后,截取當(dāng)前活動(dòng)窗口的畫面到剪切板里。

在這里插入圖片描述

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

在這里插入圖片描述

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

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

在這里插入圖片描述

1.4 ffmpeg

ffmpeg -i “輸入視頻” -fflags nobuffer -t 60 -ss 0 “輸出地址”
說明:代表截取輸入視頻從0秒到60秒的片段,保存到輸出地址。
-ss n : 起始時(shí)間為第n秒
-t n : 總共截取的片段時(shí)長為n秒

運(yùn)行后會(huì)生成截圖: out1.jpg out2.jpg out3.jpg …

ffmpeg -i fight.mp4 -r 1 -t 200 -ss 1 -f image2 out%d.jpg

1.5 ScreenToGif

在這里插入圖片描述

1.6 Chrome

在這里插入圖片描述

2、C++、GDI

2.1 微軟官方例子

https://docs.microsoft.com/en-us/windows/win32/gdi/capturing-an-image

int CaptureAnImage(HWND hWnd)
{
    HDC hdcScreen;
    HDC hdcWindow;
    HDC hdcMemDC = NULL;
    HBITMAP hbmScreen = NULL;
    BITMAP bmpScreen;
    DWORD dwBytesWritten = 0;
    DWORD dwSizeofDIB = 0;
    HANDLE hFile = NULL;
    char* lpbitmap = NULL;
    HANDLE hDIB = NULL;
    DWORD dwBmpSize = 0;

    // Retrieve the handle to a display device context for the client 
    // area of the window. 
    hdcScreen = GetDC(NULL);
    hdcWindow = GetDC(hWnd);

    // Create a compatible DC, which is used in a BitBlt from the window DC.
    hdcMemDC = CreateCompatibleDC(hdcWindow);

    if (!hdcMemDC)
    {
        MessageBox(hWnd, L"CreateCompatibleDC has failed", L"Failed", MB_OK);
        goto done;
    }

    // Get the client area for size calculation.
    RECT rcClient;
    GetClientRect(hWnd, &rcClient);

    // This is the best stretch mode.
    SetStretchBltMode(hdcWindow, HALFTONE);

    // The source DC is the entire screen, and the destination DC is the current window (HWND).
    if (!StretchBlt(hdcWindow,
        0, 0,
        rcClient.right, rcClient.bottom,
        hdcScreen,
        0, 0,
        GetSystemMetrics(SM_CXSCREEN),
        GetSystemMetrics(SM_CYSCREEN),
        SRCCOPY))
    {
        MessageBox(hWnd, L"StretchBlt has failed", L"Failed", MB_OK);
        goto done;
    }

    // Create a compatible bitmap from the Window DC.
    hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);

    if (!hbmScreen)
    {
        MessageBox(hWnd, L"CreateCompatibleBitmap Failed", L"Failed", MB_OK);
        goto done;
    }

    // Select the compatible bitmap into the compatible memory DC.
    SelectObject(hdcMemDC, hbmScreen);

    // Bit block transfer into our compatible memory DC.
    if (!BitBlt(hdcMemDC,
        0, 0,
        rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
        hdcWindow,
        0, 0,
        SRCCOPY))
    {
        MessageBox(hWnd, L"BitBlt has failed", L"Failed", MB_OK);
        goto done;
    }

    // Get the BITMAP from the HBITMAP.
    GetObject(hbmScreen, sizeof(BITMAP), &bmpScreen);

    BITMAPFILEHEADER   bmfHeader;
    BITMAPINFOHEADER   bi;

    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;

    // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that 
    // call HeapAlloc using a handle to the process's default heap. Therefore, GlobalAlloc and LocalAlloc 
    // have greater overhead than HeapAlloc.
    hDIB = GlobalAlloc(GHND, dwBmpSize);
    lpbitmap = (char*)GlobalLock(hDIB);

    // Gets the "bits" from the bitmap, and copies them into a buffer 
    // that's pointed to by lpbitmap.
    GetDIBits(hdcWindow, hbmScreen, 0,
        (UINT)bmpScreen.bmHeight,
        lpbitmap,
        (BITMAPINFO*)&bi, DIB_RGB_COLORS);

    // A file is created, this is where we will save the screen capture.
    hFile = CreateFile(L"captureqwsx.bmp",
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);

    // Add the size of the headers to the size of the bitmap to get the total file size.
    dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    // Offset to where the actual bitmap bits start.
    bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);

    // Size of the file.
    bmfHeader.bfSize = dwSizeofDIB;

    // bfType must always be BM for Bitmaps.
    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);

    // Unlock and Free the DIB from the heap.
    GlobalUnlock(hDIB);
    GlobalFree(hDIB);

    // Close the handle for the file that was created.
    CloseHandle(hFile);

    // Clean up.
done:
    DeleteObject(hbmScreen);
    DeleteObject(hdcMemDC);
    ReleaseDC(NULL, hdcScreen);
    ReleaseDC(hWnd, hdcWindow);

    return 0;
}

2.2 C++、GDI、CImage

HDC hdcSrc = GetDC(NULL);
int nBitPerPixel = GetDeviceCaps(hdcSrc, BITSPIXEL);
int nWidth = GetDeviceCaps(hdcSrc, HORZRES);
int nHeight = GetDeviceCaps(hdcSrc, VERTRES);
CImage image;
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

void CaptureOpenGLWindow(const char* savePath, int w, int h)
{
	GLubyte* pPixelData; 
	GLint    PixelDataLength;  

	// 分配內(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);

	int iw = w, ih = h, n = 3;
	stbi_set_flip_vertically_on_load(true);
	unsigned char *idata = stbi_load(savePath, &iw, &ih, &n, 0);
	stbi_write_png(savePath, w, h, 3, idata, 0);
	stbi_image_free(idata);

}

4、C++、OpenCV

BITMAPINFOHEADER createBitmapHeader(int width, int height)
{
   BITMAPINFOHEADER  bi;

     // create a bitmap
     bi.biSize = sizeof(BITMAPINFOHEADER);
     bi.biWidth = width;
     bi.biHeight = -height;  //this is the line that makes it draw upside down or not
     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;

     return bi;
}

Mat captureScreenMat(HWND hwnd)
{
     Mat src;

     // get handles to a device context (DC)
     HDC hwindowDC = GetDC(hwnd);
     HDC hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
     SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);

     // define scale, height and width
     int screenx = GetSystemMetrics(SM_XVIRTUALSCREEN);
     int screeny = GetSystemMetrics(SM_YVIRTUALSCREEN);
     int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
     int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);

     // create mat object
     src.create(height, width, CV_8UC4);

     // create a bitmap
     HBITMAP hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
     BITMAPINFOHEADER bi = createBitmapHeader(width, height);

     // use the previously created device context with the bitmap
     SelectObject(hwindowCompatibleDC, hbwindow);

     // copy from the window device context to the bitmap device context
     StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, screenx, screeny, width, height, SRCCOPY);  //change SRCCOPY to NOTSRCCOPY for wacky colors !
     GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);            //copy from hwindowCompatibleDC to hbwindow

     // avoid memory leak
     DeleteObject(hbwindow);
     DeleteDC(hwindowCompatibleDC);
     ReleaseDC(hwnd, hwindowDC);

     return src;
 }
 int main()
{
      // capture image
      HWND hwnd = GetDesktopWindow();
      Mat src = captureScreenMat(hwnd);

      // save img
      cv::imwrite("Screenshot.png", src);

  	  // clean-ups
      buf.clear();
      return 0;
}

5、C++、QT

QDesktopWidget *desk = QApplication::desktop();
QScreen * screen = QGuiApplication::primaryScreen();
QPixmap p = screen->grabWindow(desk->winId());
QImage image = p.toImage();

到此這篇關(guān)于C++實(shí)現(xiàn)截圖截屏的示例代碼的文章就介紹到這了,更多相關(guān)C++ 截圖截屏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解

    關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解

    今天小編就為大家分享一篇關(guān)于關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 詳解C++異常處理(try catch throw)完全攻略

    詳解C++異常處理(try catch throw)完全攻略

    這篇文章主要介紹了詳解C++異常處理(try catch throw)完全攻略,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 淺析C++中的多線程編程

    淺析C++中的多線程編程

    這篇文章主要為大家詳細(xì)介紹了C++中的多線程編程,包括創(chuàng)建線程、同步線程、傳遞數(shù)據(jù)給線程以及異常處理等方面,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • 數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解

    數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 定義vim配置文件vimrc用于c/c++編程

    定義vim配置文件vimrc用于c/c++編程

    vim作為Linux下廣受贊譽(yù)的代碼編輯器,其獨(dú)特的純命令行操作模式可以很大程度上方便編程工作,通過自定義vim配置文件可以實(shí)現(xiàn)對(duì)vim功能的個(gè)性化設(shè)置。這篇文章主要介紹了定義vim配置文件vimrc,用于c/c++編程 ,需要的朋友可以參考下
    2018-10-10
  • C++指向類成員的指針詳解

    C++指向類成員的指針詳解

    指向類成員的指針總的來講可以分為兩大類四小類(指向數(shù)據(jù)成員還是成員函數(shù),指向普通成員還是靜態(tài)成員),希望本片文章能給你帶來幫助
    2021-09-09
  • C語言實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)課程設(shè)計(jì)

    C語言實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)課程設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • C++11學(xué)習(xí)之多線程的支持詳解

    C++11學(xué)習(xí)之多線程的支持詳解

    這篇文章主要為大家詳細(xì)介紹了C++11中多線程支持的相關(guān)資料,文中的示例代碼講解詳細(xì),對(duì)我們深入了解C++11有一定的幫助,需要的可以參考一下
    2023-02-02
  • 一篇文章讓你徹底明白c++11增加的變參數(shù)模板

    一篇文章讓你徹底明白c++11增加的變參數(shù)模板

    C++11的新特性--可變模版參數(shù)(variadic templates)是C++11新增的最強(qiáng)大的特性之一,它對(duì)參數(shù)進(jìn)行了高度泛化,它能表示0到任意個(gè)數(shù)、任意類型的參數(shù),這篇文章主要給大家詳細(xì)介紹了關(guān)于c++11增加的變參數(shù)模板的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • C語言malloc與calloc區(qū)別詳解

    C語言malloc與calloc區(qū)別詳解

    本文主要介紹了C語言malloc與calloc區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01

最新評(píng)論