char * DibToStr( HGLOBAL hDib, long * plSize )
{
LPBITMAPINFO Info = ( LPBITMAPINFO ) GlobalLock( hDib );
void * Bits = ( void * ) ( ( char * ) Info + Info->bmiHeader.biSize +
wDIBColors( ( LPBITMAPINFOHEADER ) Info ) * sizeof( RGBQUAD ) );
BITMAPFILEHEADER bmf;
char * pDib;
long lSize;
lSize = GlobalSize( ( HGLOBAL ) Info );
pDib = hb_xgrab( sizeof( bmf ) + lSize );
bmf.bfType = 0x4D42;
bmf.bfSize = sizeof( bmf ) + lSize;
bmf.bfReserved1 = 0;
bmf.bfReserved2 = 0;
bmf.bfOffBits = sizeof( bmf ) + ( LONG ) Bits - ( LONG ) Info;
memcpy( pDib, &bmf, sizeof( bmf ) );
memcpy( ( char * ) pDib + sizeof( bmf ), ( void * ) Info, lSize );
lSize += sizeof( BITMAPFILEHEADER );
*plSize = lSize;
return pDib;
}
BOOL DibWrite( LPSTR szFileName, HGLOBAL hDIB )
{
long lSize;
char * pDib;
int hBmp;
pDib = DibToStr( hDIB, &lSize );
#ifndef UNICODE
if( ( hBmp = _lcreat( szFileName, 0 ) ) != HFILE_ERROR )
{
_hwrite( hBmp, ( const char * ) pDib, lSize );
_lclose( hBmp );
GlobalUnlock( hDIB );
return TRUE;
}
#else
if( ( hBmp = hb_fsCreate( ( unsigned char * ) szFileName, 0 ) ) != ( int ) INVALID_HANDLE_VALUE )
{
hb_fsWriteLarge( hBmp, ( unsigned char * ) pDib, lSize );
hb_fsClose( hBmp );
GlobalUnlock( hDIB );
return TRUE;
}
#endif
else
{
GlobalUnlock( hDIB );
return FALSE;
}
}
char * DibToStr( HGLOBAL hDib, long * plSize );
HB_FUNC( BMPTOSTR ) // hBmp --> cBitmapAsString
{
HGLOBAL hDib = ( HGLOBAL ) DibFromBitmap( ( HBITMAP ) hb_parnl( 1 ), 0, 0, NULL );
long lSize;
char * pDib = DibToStr( hDib, &lSize );
hb_retclen( pDib, lSize );
GlobalUnlock( hDib );
GlobalFree( hDib );
hb_xfree( pDib );
}
#pragma BEGINDUMP
#include <windows.h>
#include <hbapi.h>
WORD far pascal wDIBColors( LPBITMAPINFOHEADER );
HANDLE DibFromBitmap( HBITMAP, DWORD, WORD, HPALETTE );
char * DibToStr( HGLOBAL hDib, long * plSize )
{
LPBITMAPINFO Info = ( LPBITMAPINFO ) GlobalLock( hDib );
void * Bits = ( void * ) ( ( char * ) Info + Info->bmiHeader.biSize +
wDIBColors( ( LPBITMAPINFOHEADER ) Info ) * sizeof( RGBQUAD ) );
BITMAPFILEHEADER bmf;
char * pDib;
long lSize;
lSize = GlobalSize( ( HGLOBAL ) Info );
pDib = hb_xgrab( sizeof( bmf ) + lSize );
bmf.bfType = 0x4D42;
bmf.bfSize = sizeof( bmf ) + lSize;
bmf.bfReserved1 = 0;
bmf.bfReserved2 = 0;
bmf.bfOffBits = sizeof( bmf ) + ( LONG ) Bits - ( LONG ) Info;
memcpy( pDib, &bmf, sizeof( bmf ) );
memcpy( ( char * ) pDib + sizeof( bmf ), ( void * ) Info, lSize );
lSize += sizeof( BITMAPFILEHEADER );
*plSize = lSize;
return pDib;
}
BOOL DibWrite( LPSTR szFileName, HGLOBAL hDIB )
{
long lSize;
char * pDib;
int hBmp;
pDib = DibToStr( hDIB, &lSize );
#ifndef UNICODE
if( ( hBmp = _lcreat( szFileName, 0 ) ) != HFILE_ERROR )
{
_hwrite( hBmp, ( const char * ) pDib, lSize );
_lclose( hBmp );
GlobalUnlock( hDIB );
return TRUE;
}
#else
if( ( hBmp = hb_fsCreate( ( unsigned char * ) szFileName, 0 ) ) != ( int ) INVALID_HANDLE_VALUE )
{
hb_fsWriteLarge( hBmp, ( unsigned char * ) pDib, lSize );
hb_fsClose( hBmp );
GlobalUnlock( hDIB );
return TRUE;
}
#endif
else
{
GlobalUnlock( hDIB );
return FALSE;
}
}
HB_FUNC( BMPTOSTR ) // hBmp --> cBitmapAsString
{
HGLOBAL hDib = ( HGLOBAL ) DibFromBitmap( ( HBITMAP ) hb_parnl( 1 ), 0, 0, NULL );
long lSize;
char * pDib = DibToStr( hDib, &lSize );
hb_retclen( pDib, lSize );
GlobalUnlock( hDib );
GlobalFree( hDib );
hb_xfree( pDib );
}
HB_FUNC( DIBWRITE ) // ( cFileName, hDib ) --> lSuccess
{
hb_retl( DibWrite( ( char * ) hb_parc( 1 ), ( HGLOBAL ) hb_parnl( 2 ) ) );
}
#pragma ENDDUM
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 86 guests