Available in the LEADTOOLS Imaging toolkit. |
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 accomodate 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 variables are member variables of the CVCsampleDlg class
//void * m_pMemAddress
void CVCsampleDlg::SampleLoadSaveBuffer(char *pszFileName)
{
long lMemInitialSize = 1;
int nRet;
m_pMemAddress = malloc(lMemInitialSize);
m_Lead1.Load(pszFileName, 0, 0, 1);
m_Lead1.SetSaveBufferSize(lMemInitialSize);
m_Lead1.SetSaveBufferAddress((long)m_pMemAddress);
m_Lead1.SetEnableSaveBufferEvent(TRUE);
nRet = m_Lead1.SaveBuffer(FILE_TIF, 24, 0, SAVE_OVERWRITE);
if (nRet != 0)
{
CString csMsg;
csMsg.Format("SaveBuffer Error: %d\n", nRet);
MessageBox(csMsg);
return;
}
//Load the file in memory into a second LEAD control
nRet = m_Lead2.LoadBuffer (m_Lead1.GetSaveBufferAddress (), 24, 0, 1, m_Lead1.GetSaveBufferSize ());
free(m_pMemAddress);
}
//The SaveBuffer event allows the user to reallocate the buffer as needed
void CVCsampleDlg::OnSaveBufferLeadctrl1 (long uRequiredSize)
{
{
char szMsg[100];
wsprintf(szMsg, "uRequiredSize[%d]\n", uRequiredSize);
OutputDebugString(szMsg);
}
m_pMemAddress = realloc(m_pMemAddress, uRequiredSize);
if (m_pMemAddress == NULL)
{
MessageBox( "realloc error");
m_Lead1.SetEnableSaveBufferEvent (FALSE);
}
else
{
m_Lead1.SetSaveBufferSize(uRequiredSize);
m_Lead1.SetSaveBufferAddress ((long)m_pMemAddress);
}
}