SaveBuffer example for C++ Builder
void __fastcall TForm1::Button1Click(TObject *Sender)
{
void * pBuffer;
unsigned int uImageSize;
unsigned int uFinalBufSize;
int nRet;
int hf;
// Loading an image
LEADImage1->Load ( "v:\\images\\image1.cmp", 0, 1, 1 ) ;
// Calculate the Image Size so we can can allocate enough Buffer to hold it.
uImageSize= ceil((LEADImage1->BitmapWidth * LEADImage1->BitmapHeight * 24)/8);
// Allocate a buffer to hold the Bitmap
pBuffer= (void*) malloc (uImageSize) ;
if ( !pBuffer)
{
ShowMessage ( "Not enough Memory" ) ;
return;
}
// Saving the Bitmap in Memory
nRet = LEADImage1->SaveBuffer (pBuffer, uImageSize, FILE_CMP, 24, 2, &uFinalBufSize );
if ( nRet == SUCCESS )
{
// Create a file on Disk
hf = FileCreate("c:\\temp\\SaveBuf.cmp");
if (hf == -1)
{
free ( pBuffer ) ;
MessageDlg ("Can not create file", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
// Writing the File in Memory to Disk
if( FileWrite(hf, pBuffer, uFinalBufSize) != (int)uFinalBufSize)
MessageDlg ("Error calling FileWrite", mtError, TMsgDlgButtons() << mbOK, 0);
else
{
MessageDlg ("The file was saved successfully on disk.", mtInformation, TMsgDlgButtons() << mbOK, 0);
nRet= SUCCESS;
}
FileClose (hf);
if (pBuffer)
free ( pBuffer );
// Loading the Saved Bitmap from Disk
if ( nRet == SUCCESS )
LEADImage2->Load ("c:\\temp\\SaveBuf.cmp", 0, 1, 1 ) ;
return;
}
// Free the Buffer
if (pBuffer)
free ( pBuffer );
}