Quantcast

WinAPI, save screenshot into tiff

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

WinAPI, save screenshot into tiff

Urbach, Marcel [Rohmann GmbH]

 

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 );

    }

}


_______________________________________________
Tiff mailing list: [hidden email]
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: WinAPI, save screenshot into tiff

Paavo Helde

On 13.12.2016 16:42, Urbach, Marcel [Rohmann GmbH] wrote:

 

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:

 

        TIFFSetField( tiff, TIFFTAG_BITSPERSAMPLE, info.bmiHeader.biBitCount );

biBitCount is 24 for typical RGB images, but you need to pass 8 to libtiff as this number is "per sample".

Cheers
Paavo


_______________________________________________
Tiff mailing list: [hidden email]
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
Loading...