SaveMemory example for C++ Builder
This example uses SaveMemory to save a bitmap to a memory-resident file, uses GetMemoryInfo to examine the file size; then uses WIN32 API calls to save the file to disk and free the memory.
long hf;
long hMem;
long iSize;
void * lpMem;
AnsiString cMsg;
LEADRasterIO* pRasterIO= NULL;
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
hMem= 0;
iSize= 0;
if (pRasterIO->SaveMemory (LEADRasterView1->Raster, &hMem, FILE_CMP, 24, (QFactorConstants)-1,
SAVE_OVERWRITE, &iSize ))
{
ShowMessage("Error calling SaveMemory");
return;
}
if (pRasterIO->GetMemoryInfo (LEADRasterView1->Raster, hMem, 0, iSize))
{
GlobalFree ((HGLOBAL)hMem);
return;
}
//Ask for a confirmation to save this file
cMsg= "File info:\n" ;
cMsg= cMsg + IntToStr((int)pRasterIO->InfoWidth) + " x " + IntToStr((int)pRasterIO->InfoHeight);
cMsg= cMsg + " x " + IntToStr(pRasterIO->InfoBits) + " BPP\n" ;
cMsg= cMsg + "Size in memory: " + IntToStr((int)pRasterIO->InfoSizeMem) + "\n";
cMsg= cMsg + "Size on disk : " + IntToStr((int)pRasterIO->InfoSizeDisk);
if (MessageBox(Handle, cMsg.c_str(),"Write this file on disk ", MB_YESNO) == IDNO )
{
GlobalFree ((HGLOBAL)hMem);
return;
}
if( (lpMem = (LPSTR)GlobalLock((HGLOBAL)hMem)) == NULL )
{
GlobalFree ((HGLOBAL)hMem);
ShowMessage("Error calling GlobalLock");
return;
}
hf = FileCreate("d:\\pictures\\savemem.cmp");
if (hf == -1)
{
GlobalUnlock ((HGLOBAL)hMem);
GlobalFree ((HGLOBAL)hMem);
ShowMessage ("Error calling OpenFile");
return;
}
if (FileWrite(hf, lpMem, iSize) != iSize )
MessageDlg ("Error calling FileWrite", mtError, TMsgDlgButtons() << mbOK, 0);
else
MessageDlg ("The file was saved successfully on disk.", mtInformation, TMsgDlgButtons() << mbOK, 0);
pRasterIO-> Release( );
FileClose (hf);
GlobalUnlock ((HGLOBAL)hMem);
GlobalFree ((HGLOBAL)hMem);