🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

from DC to DIB

Started by
3 comments, last by Matthew Allen 24 years, 5 months ago
I''m attempting to make a a program that takes a number of files and copies a part of them to another bitmap. This is for getting a peice of several rendered 3D images and lining them up in a single image for sprite animation. I want to do it in Win32... I''ve programmed all of it EXCEPT code to copy a Device Context to a DIB. The bitmap that I want to save is in an HDC, but I need to have it in a DIB to save it. Is there any quick way to do this. I suppose there''s a GDI function that does it, but I can''t find it. Thanks for the help! - mallen22@concentric.net
- http://mxf_entertainment.tripod.com/
Advertisement
You have to create a a temporay DC that is compatible with the screen and Using SelectObject to select the DIB into the new DC, then use BitBlt to perform the copy.

Domini
Is there anything obviously wrong with this code:

////////// CREATE THE COMPOSITE BITMAP
// Get a device context to the screen.
if ( ( SurfDC = GetDC( NULL ) ) == NULL )
return Error( "GetDC FAILED", "CROPtoPPP" );

// Make a device context for the composite bitmap.
if ( ( MemDC = CreateCompatibleDC( SurfDC ) ) == NULL )
return Error( "CreateCompatibleDC FAILED", "CROPtoPPP" );

// Allocate memory for the bitmap header.
if ( ( pBMPInfo = ( PBITMAPINFO ) LocalAlloc( LPTR, sizeof( BITMAPINFOHEADER ) ) ) == NULL )
return Error( "LocalAlloc FAILED", "CROPtoPPP" );

// Set the bitmap header's values.
pBMPInfo->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
pBMPInfo->bmiHeader.biWidth = nPeiceWidth*nWidth;
pBMPInfo->bmiHeader.biHeight = nPeiceHeight*nHeight;
pBMPInfo->bmiHeader.biPlanes = 1;
pBMPInfo->bmiHeader.biBitCount = 24;
pBMPInfo->bmiHeader.biCompression = BI_RGB;
pBMPInfo->bmiHeader.biSizeImage = ( pBMPInfo->bmiHeader.biWidth + 7 ) / 8 * pBMPInfo->bmiHeader.biHeight * 24;
pBMPInfo->bmiHeader.biClrImportant = 0;

// Create the composite bitmap.
if ( ( hCompositeBMP = CreateDIBitmap( MemDC, ( const struct tagBITMAPINFOHEADER * )pBMPInfo, 0, 0, 0, DIB_RGB_COLORS ) ) == NULL )
return Error( "CreateDIBitmap FAILED", "CROPtoPPP" );


////////// LOAD THE BITMAPS INTO THE COMPOSITE
// Select the composite bitmap into the memory Device Context.
if ( SelectObject( MemDC, hCompositeBMP ) == ( void * )GDI_ERROR )
return Error( "SelectObject FAILED", "CROPtoPPP" );

// Go through the bitmaps, crop them, and put the needed part into the composite bitmap.
for ( int nCountH = nHeight; nCountH > 0; nCountH-- )
for ( int nCountW = nWidth; nCountW > 0; nCountW-- )
{
char szFilename[10]; // The file name of the uncropped bitmap.

// Set the file name. The first and second bitmap in the first row are "1-1.bmp" and "1-2.bmp", for example.
_itoa( nCountH, &szFilename[0], 10 );
szFilename[1] = '-';
_itoa( nCountW, &szFilename[2], 10 );
strcat( szFilename, ".bmp" );

// Load an uncropped bitmap.
if ( ( hBMP = ( HBITMAP ) LoadImage( NULL, szFilename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE / LR_CREATEDIBSECTION ) ) == NULL )
return Error( "LoadImage 2 FAILED", "CROPtoPPP" );

// Select it intot the Surface DC.
if ( SelectObject( SurfDC, hBMP ) == ( void * )GDI_ERROR )
return Error( "SelectObject FAILED", "CROPtoPPP" );

// Blit the needed part into the composite bitmap.
if ( BitBlt( MemDC, nPeiceWidth*nCountW, nPeiceHeight*nCountH, nPeiceWidth, nPeiceHeight, SurfDC, x, y, SRCCOPY ) == FALSE )
return Error( "BitBlt FAILED", "CROPtoPPP" );

// Delete the bitmap so another can be loaded.
if ( DeleteObject( hBMP ) == FALSE )
return Error( "DeleteObject FAILED for hBMP", "CROPtoPPP" );
}

// Release the DC so Windows can use it.
if ( ReleaseDC( NULL, SurfDC ) == FALSE )
return Error( "ReleaseDC FAILED", "CROPtoPPP" );


////////// SETUP THE BUFFERS
// Set up the buffers used to compress the bitmap into a PPP file.
if ( ( destBuffer = ( LPBYTE ) GlobalAlloc( GMEM_FIXED, pBMPInfo->bmiHeader.biSizeImage ) ) == NULL )
return Error( "GlobalAlloc FAILED for destBuffer", "CROPtoPPP" );

if ( ( comprBuffer = ( LPBYTE ) GlobalAlloc( GMEM_FIXED, pBMPInfo->bmiHeader.biSizeImage ) ) == NULL )
return Error( "GlobalAlloc FAILED for comprBuffer", "CROPtoPPP" );


////////// LOAD THE BITMAP DATA INTO THE BUFFER
// Load the bitmap's data into the buffer. la la la...
if ( GetDIBits( MemDC, hCompositeBMP, 0, ( unsigned int ) nPeiceHeight*nHeight, destBuffer, pBMPInfo, 0 ) == FALSE )
return Error( "GetDIBits FAILED", "CROPtoPPP" );

- mallen22@concentric.net
- http://mxf_entertainment.tripod.com/

Edited by - Matthew Allen on 1/17/00 11:29:45 AM
It looks like you need a good algorism, seriously that will allow you to access the bitmap before it (supposidly) is converted which will cause this to really allow you to formally change the format, it won''t be a bmp anymore or a pnp, it is a ppp now, because it would be fucked in the ass, i mean umm done... or something....
*bump*
Does anyone know any way to help me with this? The code that causes the problem is above... Thanks!

- mallen22@concentric.net
- http://mxf_entertainment.tripod.com/

This topic is closed to new replies.

Advertisement