服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

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++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30