I want to take a screenshot in C on my windows machine and save it as a jpg or bmp or whatever. Anyway, I tried to do it by my own, it's okay and working well but it's UNBEARABLY SLOW, unlike prt scr
key - I wonder if there's a way to access prt scr
clipboard and somehow paste it in a jpg/png file OR if there's a faster way to get all of the screen pixels.
That's my code:
int main()
{
bitmap_t pic;
int i, j;
pic.width = GetSystemMetrics(SM_CXSCREEN);
pic.height = GetSystemMetrics(SM_CYSCREEN);
pic.pixels = (pixel_t**)malloc(sizeof(pixel_t*)*pic.width);
for(i = 0 ; i < pic.height ; i++)
{
pic.pixels[i] = (pixel_t*)malloc(sizeof(pixel_t)*pic.height);
}
HDC hdc = GetDC(NULL);
COLORREF c;
printf("Size of your monitor is %d by %d.\n", pic.width, pic.height);
for(i = 0 ; i < pic.width ; i++)
{
for(j = 0 ; j < pic.height ; j++)
{
c = GetPixel(hdc, i, j);
pic.pixels[i][j].red = (uint8_t)GetRValue(c);
pic.pixels[i][j].green = (uint8_t)GetGValue(c);
pic.pixels[i][j].blue = (uint8_t)GetBValue(c);
}
}
ReleaseDC(NULL, hdc);
save_png_to_file(&pic, "D:\\pic.png");
for(i = 0 ; i < pic.height ; i++)
{
free(pic.pixels[i]);
}
free(pic.pixels);
return 0;
}
The function save_png_to_file
is working propely, the loop is taking too long (my screen is 1366x768 and it's over million loop entries) - why is it so slow when the key prt scr
is doing it easily?