激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C/C++ - C++實現截圖截屏的示例代碼

C++實現截圖截屏的示例代碼

2022-03-09 14:31愛看書的小沐 C/C++

本文主要介紹了C++實現截圖截屏的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

1、截圖工具

1.1 鍵盤截圖(PrtScn鍵)

如何使用Microsoft Windows操作系統中的Print Screen(打印屏幕)鍵
(1)Print Screen鍵
按下之后,截取整個屏幕的畫面到剪切板里。可以復制到其他軟件里,比如系統的畫圖工具,Office Word等。

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

C++實現截圖截屏的示例代碼

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

C++實現截圖截屏的示例代碼

按下該組合鍵之后,使用鼠標在屏幕上畫出想要截取的矩形區域,自動保存到系統剪切板里。

1.3 系統自帶的截圖小工具

C++實現截圖截屏的示例代碼

1.4 ffmpeg

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

運行后會生成截圖: out1.jpg out2.jpg out3.jpg …

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

1.5 ScreenToGif

C++實現截圖截屏的示例代碼

1.6 Chrome

C++實現截圖截屏的示例代碼

 

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;  

	// 分配內存和打開文件
	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();

到此這篇關于C++實現截圖截屏的示例代碼的文章就介紹到這了,更多相關C++ 截圖截屏內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/hhy321/article/details/121548612

延伸 · 閱讀

精彩推薦
  • C/C++c++ 單線程實現同時監聽多個端口

    c++ 單線程實現同時監聽多個端口

    這篇文章主要介紹了c++ 單線程實現同時監聽多個端口的方法,幫助大家更好的理解和學習使用c++,感興趣的朋友可以了解下...

    源之緣11542021-10-27
  • C/C++學習C++編程的必備軟件

    學習C++編程的必備軟件

    本文給大家分享的是作者在學習使用C++進行編程的時候所用到的一些常用的軟件,這里推薦給大家...

    謝恩銘10102021-05-08
  • C/C++深入理解goto語句的替代實現方式分析

    深入理解goto語句的替代實現方式分析

    本篇文章是對goto語句的替代實現方式進行了詳細的分析介紹,需要的朋友參考下...

    C語言教程網7342020-12-03
  • C/C++C/C++經典實例之模擬計算器示例代碼

    C/C++經典實例之模擬計算器示例代碼

    最近在看到的一個需求,本以為比較簡單,但花了不少時間,所以下面這篇文章主要給大家介紹了關于C/C++經典實例之模擬計算器的相關資料,文中通過示...

    jia150610152021-06-07
  • C/C++詳解c語言中的 strcpy和strncpy字符串函數使用

    詳解c語言中的 strcpy和strncpy字符串函數使用

    strcpy 和strcnpy函數是字符串復制函數。接下來通過本文給大家介紹c語言中的strcpy和strncpy字符串函數使用,感興趣的朋友跟隨小編要求看看吧...

    spring-go5642021-07-02
  • C/C++C語言中炫酷的文件操作實例詳解

    C語言中炫酷的文件操作實例詳解

    內存中的數據都是暫時的,當程序結束時,它們都將丟失,為了永久性的保存大量的數據,C語言提供了對文件的操作,這篇文章主要給大家介紹了關于C語言中文件...

    針眼_6702022-01-24
  • C/C++C語言實現電腦關機程序

    C語言實現電腦關機程序

    這篇文章主要為大家詳細介紹了C語言實現電腦關機程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    xiaocaidayong8482021-08-20
  • C/C++C++之重載 重定義與重寫用法詳解

    C++之重載 重定義與重寫用法詳解

    這篇文章主要介紹了C++之重載 重定義與重寫用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下...

    青山的青6062022-01-04
主站蜘蛛池模板: 免费黄色在线观看网站 | 国人精品视频在线观看 | 91 在线观看| av手机在线免费播放 | 日韩 综合 | 美国av片在线观看 | 久久久免费观看完整版 | 国产精品久久久久久久久久久久久久久久 | 国产一级大片在线观看 | 亚洲一区二区在线免费 | 沉沦的校花奴性郑依婷c到失禁 | 精国产品一区二区三区 | 激情视频免费看 | 欧美在线成人影院 | 特一级黄色毛片 | 国产精品a一 | 久久免费视频5 | 免费看欧美一级特黄a大片 久久免费视频一区二区三区 | 国内精品久久久久久2021浪潮 | 久久精品视频12 | 国产91免费看 | 黄色av网站在线观看 | 永久免费不卡在线观看黄网站 | 国产二三区 | 国产一区精品在线观看 | xnxx 日本19| 久久99久久98精品免观看软件 | 欧美日本不卡 | 久久精品视频免费观看 | 日本欧美中文字幕 | 国产三级三级三级三级 | 精品人伦一区二区三区蜜桃网站 | av成人免费| 一本一道久久久a久久久精品91 | 毛片免费视频 | 欧美精品一区二区性色 | 九九热免费精品 | 天天躁狠狠躁夜躁2020挡不住 | 污视频在线看 | av在线免费观看中文字幕 | 久久精品国产精品亚洲 |