2016.10.25 20:02 "[Tiff] problem adding YRESOLUTION and RESOLUTIONUNIT to 16bit grayscale TIFF", by Paul Hemmer

2016.12.13 14:42 "[Tiff] WinAPI, save screenshot into tiff", by

Hello Guys
I want to capture a screenshot by using winapi only (no MFC wanted) and save it as bitmap into a tiff file.
The screen is captured correctly, but it looks like the bitmap is saved 4 times into the tiff and now looks like this:

http://imgur.com/a/CT5jw

Here is a sample of the code. Any ideas what I am doing wrong?

void CreateTiffImage2( )
{

    HDC hScreen = CreateDC( "DISPLAY", NULL, NULL, NULL );
    int width = GetDeviceCaps( hScreen, HORZRES );
    int height = GetDeviceCaps( hScreen, VERTRES );

    HDC     hDC     = CreateCompatibleDC( hScreen );
    HBITMAP hBitmap = CreateCompatibleBitmap( hScreen, width, height );

HGDIOBJ old_obj = SelectObject( hDC, hBitmap );

    BOOL    bRet    = BitBlt( hDC, 0, 0, width, height, hScreen, 0, 0, SRCCOPY );

// get bitmap infos
BITMAPINFO info ={ 0, };
info.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );

auto res = GetDIBits( hDC, hBitmap, 0, height, NULL, &info, DIB_RGB_COLORS );

// prepare buffer and get pixels

    std::vector<RGBQUAD> rgbq( width * height );
    ::GetDIBits( hDC, hBitmap, 0, height, &rgbq[0], &info, DIB_RGB_COLORS );

TIFF* tiff = TIFFOpen( "c:\\test2.tif", "w" );
if ( tiff )
{
    TIFFCreateDirectory( tiff );
    TIFFSetField( tiff, TIFFTAG_IMAGEWIDTH, info.bmiHeader.biWidth );
    TIFFSetField( tiff, TIFFTAG_IMAGELENGTH, std::abs( info.bmiHeader.biHeight ) );
    TIFFSetField( tiff, TIFFTAG_BITSPERSAMPLE, info.bmiHeader.biBitCount );
    TIFFSetField( tiff, TIFFTAG_SAMPLESPERPIXEL, 3 );

TIFFSetField( tiff, TIFFTAG_PLANARCONFIG   , PLANARCONFIG_CONTIG   );

TIFFSetField( tiff, TIFFTAG_ORIENTATION    , ORIENTATION_TOPLEFT   );

    TIFFSetField( tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB );

TIFFSetField( tiff, TIFFTAG_COMPRESSION    , COMPRESSION_LZW       );

    std::vector<BYTE> vec;
    for ( auto item: rgbq )
    {
        vec.push_back( item.rgbRed );
        vec.push_back( item.rgbGreen );
        vec.push_back( item.rgbBlue );
    }
    vec.push_back( 0 );
    vec.push_back( 0 );
    vec.push_back( 0 );

    for ( int y = 0; y < std::abs( info.bmiHeader.biHeight ); y++ )
    {

            res = TIFFWriteScanline( tiff, &vec[y * info.bmiHeader.biWidth * 3], y );

            if ( res != 1 )
                throw std::exception( "Error adding bitmap to tiff. " + res );
        }
        TIFFFlush( tiff );
    }
}