SaveBuffer example for C++ 5.0 and later
//This sample loads the file 'szFileName', and saves it to memory buffer allocated by the user
//The original memory buffer is 1 bytes, which is too small to accommodate the image
//The SaveBuffer callback gets called as necessary, allowing the user to reallocate the memory buffer as needed
//When the image is completely saved in memory, it is loaded into a second LEAD control
//The following are public class members
//HANDLE m_lHeap;
//LPVOID m_lHeapAddress;
void CTutorDlg::SampleLoadSaveBuffer(LPTSTR szFileName)
{
ILEADRasterIO *pRasterIO=NULL;
CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
DWORD dwCookie;
CRasterIOSink *pRasterIOSink;
//Instantiate the sink class and hold a pointer to it.
pRasterIOSink = new CRasterIOSink;
pRasterIOSink->m_pDlg = this;
pRasterIOSink->m_pRasterIO = pRasterIO;
//Establish a connection between source and sink.
LPUNKNOWN pUnkSink = pRasterIOSink->GetIDispatch (FALSE);
AfxConnectionAdvise(pRasterIO, DIID__LEADRasterIOEvents, pUnkSink, FALSE, &dwCookie);
long lHeapInitialSize, lMemInitialSize;
short nRet;
//Create the heap
m_lHeap = 0;
lHeapInitialSize = 1000;
m_lHeap = HeapCreate(0, lHeapInitialSize, 0);
//Allocate the first chunk of memory to be 100 bytes
m_lHeapAddress = 0;
lMemInitialSize = 1;
m_lHeapAddress = HeapAlloc(m_lHeap, 0, lMemInitialSize);
pRasterIO->Load (m_RasterView1.GetRaster (), szFileName, 0, 0, 1);
pRasterIO->SaveBufferSize= lMemInitialSize;
pRasterIO->SaveBufferAddress= long(m_lHeapAddress);
pRasterIO->EnableSaveBufferEvent= TRUE;
nRet = pRasterIO->SaveBuffer(m_RasterView1.GetRaster(), FILE_TIF, 24, QFACTOR_LEAD_0, SAVE_OVERWRITE);
if (nRet != 0)
{
CString strMsg;
strMsg.Format(TEXT("SaveBuffer Error: %d"), nRet);
AfxMessageBox(strMsg);
}
//Load the file in memory into a second LEAD Raster object
nRet = pRasterIO->LoadBuffer(m_RasterView2.GetRaster(), pRasterIO->SaveBufferAddress, 24, 0, 1, pRasterIO->SaveBufferSize);
//Free the allocated memory
HeapFree(m_lHeap, 0, m_lHeapAddress);
//Destroy the heap
HeapDestroy(m_lHeap);
//Terminate a connection between source and sink.
pUnkSink = pRasterIOSink->GetIDispatch (FALSE);
AfxConnectionUnadvise(pRasterIO, DIID__LEADRasterIOEvents, pUnkSink, FALSE, dwCookie);
delete pRasterIOSink;
pRasterIOSink = NULL;
pRasterIO->Release();
}
void CRasterIOSink::OnSaveBuffer( long uRequiredSize )
{
//The SaveBuffer event allows the user to reallocate the buffer as needed
m_pDlg->m_lHeapAddress = HeapReAlloc(m_pDlg->m_lHeap, 0, m_pDlg->m_lHeapAddress, uRequiredSize);
if(!m_pDlg->m_lHeapAddress)
{
AfxMessageBox(TEXT("HeapReAlloc Error"));
m_pRasterIO->EnableSaveBufferEvent= FALSE;
}
else
{
m_pRasterIO->SaveBufferSize = uRequiredSize;
m_pRasterIO->SaveBufferAddress = long(m_pDlg->m_lHeapAddress);
}
}