I have a problem with taking screenshots. Everything is fine, but I have a problem with it in resolution 1366x768. Bitmap is broken and I can't open this file.
HDC _dc = GetWindowDC(okno);
Szer_Wys(); //ustawia szerokosc i wysokosc aktywnego ekranu
dc = CreateCompatibleDC( 0 );
bm = CreateCompatibleBitmap( _dc, w, h );
SelectObject( dc, bm );
StretchBlt( dc, 0, 0, w, h, _dc, 0, 0, w, h, SRCCOPY );
GetObject( bm, 84, buf );
ddd = GetDC( 0 );
dc2 = CreateCompatibleDC( ddd );
tagBITMAPINFO bit_info; //tworzy obiekt na strukture
bit_info.bmiHeader.biSize = sizeof( bit_info.bmiHeader ); //Rozmiar struktury bmiHeader (40 )
bit_info.bmiHeader.biWidth = w; //Szerokość bitmapy w pikselac
bit_info.bmiHeader.biHeight = h; //wysokosc bitmapy w pikselac
bit_info.bmiHeader.biPlanes = 1;
bit_info.bmiHeader.biBitCount = 24; //Liczba bitów kodujących piksel (RGB)
bit_info.bmiHeader.biCompression = 0; //0 - brak wewnętrznej kompresji
bit_info.bmiHeader.biSizeImage = 0;
h_createDIB = CreateDIBSection( dc, & bit_info, DIB_RGB_COLORS, & buf, 0, 0 );
GetDIBits( dc, bm, 0, h, buf, & bit_info, DIB_RGB_COLORS );
BITMAPFILEHEADER bit_header;
bit_header.bfType = MAKEWORD( 'B', 'M' ); //typ pliku
bit_header.bfSize = w * h * 3 + 54; //w * h * 3 kolory + nagłówek
bit_header.bfOffBits = 54;
BITMAPINFOHEADER bit_info_header;
bit_info_header.biSize = 40;
bit_info_header.biWidth = w;
bit_info_header.biHeight = h;
bit_info_header.biPlanes = 0;
bit_info_header.biBitCount = 24;
bit_info_header.biCompression = 0;
bit_info_header.biSizeImage = w * h * 3;
sciezka = obiekt.PobierzPathSCREEN() + obiekt.PobierzNazwe() + ".bmp";
ofstream plik(sciezka, ios::binary);
if(plik.good())
{
plik.write(reinterpret_cast<char*>(&bit_header), sizeof(bit_header));
plik.write(reinterpret_cast<char*>(&bit_info_header), sizeof(bit_info_header));
plik.write(reinterpret_cast<char*>(buf), w*h*3 );
}else cout<<"Blad pliku";
plik.close();
*Szer_Wys()* is reading screen resolution.
EDIT
I changed the content of Szer_Wys() to this:
void Zrzut::Szer_Wys()
{
RECT re;
GetWindowRect( okno, & re );
w = re.right, h = re.bottom;
while(w%4 != 0)
{
w++;
}
}
And now everything works fine :)