title: How to turn a screenshot bitmap to a cv::Mat
body: <p>I currently am trying to take a screenshot of the screen, and then get it into a format editable by OpenCV. The code I'm using is from the microsoft website, <a href="https://docs.microsoft.com/en-gb/windows/win32/gdi/capturing-an-image" rel="nofollow noreferrer">https://docs.microsoft.com/en-gb/windows/win32/gdi/capturing-an-image</a>. The code uses the "Windows.h" library. The easiest way of doing it is obviously to just save the bitmap as a .bmp, then open it using opencv. However, I would like it to be more efficient than that, and I don't know how to. When I used the code, it outputted a char pointer, which I don't know how to convert to a cv::Mat. The code is below:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>cv::Mat * Capture::GetMat()
  {
      cv::Mat * mat1;
  
      MemoryHandle = NULL;
      BitmapHandle = NULL;
  
      // Find the handle for the device context of the entire screen, and the specific window specified.
      ScreenHandle = GetDC(NULL);
      WindowHandle = GetDC(hwnd);
  
      //Make the compatible DC (Device Context) for storing the data in memory.
      MemoryHandle = CreateCompatibleDC(WindowHandle);
  
      //Make a compatible DC for the bitmap to be stored in.
      BitmapHandle = CreateCompatibleBitmap(WindowHandle, width, height);
  
      //Select the correct bitmap, and put it into memory using the memory handle.
      SelectObject(MemoryHandle, BitmapHandle);
  
      //Transfer the actual bitmap into the compatible memory DC.
      BitBlt(MemoryHandle, 0, 0, 1920, 1080, WindowHandle, 0, 0, SRCCOPY);
  
      //Get the bitmap from the handle, and ready it to be filed.
      GetObject(BitmapHandle, sizeof(BITMAP), &amp;Bitmap);
  
      //Cofinguring INFO details.
      bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
      bmpInfoHeader.biWidth = Bitmap.bmWidth;
      bmpInfoHeader.biHeight = Bitmap.bmHeight;
      bmpInfoHeader.biPlanes = 1;
      bmpInfoHeader.biBitCount = 32;
      bmpInfoHeader.biCompression = BI_RGB;
      bmpInfoHeader.biSizeImage = 0;
      bmpInfoHeader.biXPelsPerMeter = 0;
      bmpInfoHeader.biYPelsPerMeter = 0;
      bmpInfoHeader.biClrUsed = 0;
      bmpInfoHeader.biClrImportant = 0;
  
      bmpSize = ((Bitmap.bmWidth * bmpInfoHeader.biBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
  
      memhnd = GlobalAlloc(GHND, bmpSize);
      mat1 = (cv::Mat *)GlobalLock(memhnd);
      std::cout &lt;&lt; GetLastError() &lt;&lt; std::endl;
  
      return mat1;
  }
  </code></pre>
  
  <pre class="lang-cpp prettyprint-override"><code>int Capture::save_mat(cv::Mat * mat)
  {
      std::string FileName("P:/Photos/capture");
      FileName += std::to_string(image_count_mat);
      FileName += (const char*)(".jpg");
      cv::Mat mat2 = *mat;
      cv::imwrite(FileName.c_str(), mat2);
      image_count_mat++;
      return 0;
  }
  </code></pre>
  
  <p>The class has these attributes:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>private:
          HWND hwnd;
          HDC hdc;
          int image_count_bitmap = 0;
          int image_count_mat = 0;
          int height;
          int width;
  
          HDC ScreenHandle;
          HDC WindowHandle;
          HDC MemoryHandle = NULL;
          HBITMAP BitmapHandle = NULL;
          BITMAP Bitmap;
  
          BITMAPFILEHEADER bmpFileHeader;
          BITMAPINFOHEADER bmpInfoHeader;
  
          DWORD bmpSize;
          HANDLE memhnd;
  </code></pre>
  
  <p>The GetMat() function works fine and doesn't output an error, although I have no idea how to check if the outputted cv::Mat is correct. When I run the save_mat() function however, the program crashes.</p>