0

画面上の画像を見つける必要があります。簡単な比較ループを作成することにしました。

役立つと思われるこの回答を見つけて、次のコードを書きました。

void Capt()
{
  HDC hdcSource = GetDC(GetDesktopWindow()); // the source device context
  HDC hdc = CreateCompatibleDC(hdcSource);
  HBITMAP hSource = CreateCompatibleBitmap(hdcSource, xw, yw); // the bitmap selected into the device context

  SelectObject(hdc, hSource);

  BITMAPINFO MyBMInfo = { 0 };
  MyBMInfo.bmiHeader.biSize = sizeof(MyBMInfo.bmiHeader);

  // Get the BITMAPINFO structure from the bitmap
  if (0 == GetDIBits(hdc, hSource, 0, 0, NULL, &MyBMInfo, DIB_RGB_COLORS))
  {
    mb("error1");
    IA(GetLastError());
  }

  // create the pixel buffer
  BYTE* lpPixels = new BYTE[MyBMInfo.bmiHeader.biSizeImage];

  // We'll change the received BITMAPINFOHEADER to request the data in a
  // 32 bit RGB format (and not upside-down) so that we can iterate over
  // the pixels easily. 

  // requesting a 32 bit image means that no stride/padding will be necessary,
  // although it always contains an (possibly unused) alpha channel
  MyBMInfo.bmiHeader.biBitCount = 32;
  MyBMInfo.bmiHeader.biCompression = BI_RGB;  // no compression -> easier to use
  // correct the bottom-up ordering of lines (abs is in cstdblib and stdlib.h)
  MyBMInfo.bmiHeader.biHeight = abs(MyBMInfo.bmiHeader.biHeight);

  // Call GetDIBits a second time, this time to (format and) store the actual
  // bitmap data (the "pixels") in the buffer lpPixels
  if (0 == GetDIBits(hdc, hSource, 0, MyBMInfo.bmiHeader.biHeight,
    lpPixels, &MyBMInfo, DIB_RGB_COLORS))
  {
    mb("error2");
    IA(GetLastError());
  }

  DeleteObject(hSource);
  ReleaseDC(NULL, hdcSource);


  for (int i = 0, j=0; i < MyBMInfo.bmiHeader.biSizeImage&&j<100; i++)
  {
    if (lpPixels[i] != 0)
    {
      char buf[1024] = {};
      _itoa_s(lpPixels[i], buf, 10);
  
      //output
    }
  }
}

2 つの問題:

  1. 私の画面の解像度は 1280x800=1 024 000 px で、MyBMInfo.bmiHeader.biSizeImage4 096 000 に相当します。rgbですかそれとも何ですか?
  2. 主な問題: ただし、エラーはなく、前の問題で言及した値はゼロではありませんが、コードの最後のループでは、すべての値lpPixelsがゼロであり、出力が得られません。なんで?
4

1 に答える 1