1

I'd like to save my "screen" in bmp; but a think I'm doing it wrong because this code is really slow:

HDC Win = GetDC(NULL);
for (j = 0; j < y; j++)
  for (i = 0; i < x; i++)
    img->data[j][i] = GetPixel(Win, i, j);
Roman R.
  • 68,205
  • 6
  • 94
  • 158
gfauchart
  • 82
  • 10
  • define "really slow", i.e., exactly how long does it take for what sized input and how many tests did you run to get that number? Also, performance comparisons are irrelevant unless you give us your compiler settings. That said... `GetPixel` is slow. I would get a pointer to the image data itself and work on that. – Ed S. Jun 22 '12 at 20:45
  • possible duplicate of [Is there a faster alternative to GDI GetPixel()?](http://stackoverflow.com/questions/4235731/is-there-a-faster-alternative-to-gdi-getpixel) – Ed S. Jun 22 '12 at 20:47
  • I've a resolution of 1920*1080 and this code take 1minute, for a screenshot i found this Really slow – gfauchart Jun 22 '12 at 20:47

1 Answers1

6

CreateDIBSection to create a bitmap with pointer to raw bits, and BitBlt from screen into the created bitmap. This works way faster than pixel by pixel queries and you have all the data in memory accessible using regular pointer.

See Performing full screen grab in windows for a code snippet.

Community
  • 1
  • 1
Roman R.
  • 68,205
  • 6
  • 94
  • 158