Loading and Saving Images Using Databases (C++ 5.0 and later) (original) (raw)
To start a project and add code that demonstrates adding images to or deleting images from the BLOB field of a database file:
First create a database file (DB1.MDB) with the fields listed below, before running this tutorial.
Field Name | Type |
---|---|
Image | OleObject |
Size | Number |
- Start a new project as follows: Run Microsoft Visual Studio, select the File New Project, and do the following:
- Expand Visual C++ tree, if it is not already expanded.
- Select MFC from the sub tree.
- Select MFC Application from the right window.
- In the Project name text box, specify ImageBlob.
- In the Location text box, specify the path of the project.
- Click the OK button.
- In the overview window Just click Next.
- In the Application Type step dialog box, do the following:
- Select Dialog based.
- Use MFC in a shared DLL.
- Click the Next button.
- In the Step 2 of 4 dialog box, do the following:
- Ensure that About Box is selected.
- Ensure that System Menu is selected.
- Click the Next button.
- In the Step 3 of 4 dialog box, just click Finish:
- Add #include statements to your program so you can access the LEAD C++ Class Library constants and classes:
- In the Project Workspace, click the Solution Explorer tab.
- Double-click the ImageBlob Project folder to open it.
- Double-click the Header Files folder to open it.
- Double-click the StdAfx.h file to edit it.
- Add the following lines to the end of the file (keep in mind, you may have to change the path to where the header files reside):
#include "..\\..\\..\\..\\..\\include\\ClassLib\\ltWrappr.h"
- Add a static text control to the main window as follows:
- In the Project Workspace, click the ResourceView tab.
- Double-click the ImageBlob.rc folder to open it.
- Double-click the Dialog folder to open it.
- Double-click IDD_IMAGEBLOB_DIALOG to design the form.
- Select the TODO... text control; then press the Delete key to delete it.
- Click the Static Text control icon, add a control to the dialog, then size and position the control as you want it to appear at run time.
- Add #import statements to your program so you can access the Microsoft ActiveX Data Object 2.6 Library:
- In the Project Workspace, click the Solution Explorer tab.
- Double-click the ImageBlob folder to open it.
- Double-click the Header Files folder to open it.
- Double-click the StdAfx.h file to edit it.
- Add the following lines to the end of the file (keep in mind, you may have to change the path to where the .tlb files reside):
#import <C:\\\\Windows\\\\system32\\\\msado26.tlb> rename("EOF", "ADOEOF")
using namespace ADODB;
- Click the Resource View tab. On the View menu, select ToolBox, and then add six command buttons to your form and name them as follows:
| ID | Caption |
| ----------------- | ------------------------- |
| IDC_CONNECT | &Connect To Database |
| IDC_DISCONNECT | &Disconnect From Database |
| IDC_ADDRECORD | &Add Record |
| IDC_DELETERECORD | D&elete Record |
| IDC_MOVENEXT | Move &Next |
| IDC_MOVEPREVIOUS | Move &Previous |
Save the resource file.
12. Do the following to add m_btnAddRecord to the CImageBlobDlg class and link the variable to the CButton control using dynamic data exchange:
- Open ImageBlob.rc and Double-click on IDD_IMAGEBLOB_DIALOG.
- Right-click on the Add Record button, and choose Add Variable.
- Specify m_btnAddRecord as the variable name, and Control as the category.
- Click Finish to close the dialog box.
- Repeat step 11 for the other buttons, using the following variable names:
| ID | Member Variable Name |
| ----------------- | -------------------- |
| IDC_MOVEPREVIOUS | m_btnMovePrevious |
| IDC_MOVENEXT | m_btnMoveNext |
| IDC_DISCONNECT | m_btnDisconnect |
| IDC_DELETERECORD | m_btnDeleteRecord |
| IDC_CONNECT | m_btnConnect |
14. Add the following Member Variables to the CImageBlobDlg class:_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
LBitmapWindow m_LBitmapWnd;
15. Go to the OnInitDialog() function as follows:
- From view menu, select Class view.
- In the Project Workspace, click the ClassView tab.
- Double-click the ImageBlob folder to open it.
- Click on the CImageBlobDlg class.
- Double-click the _OnInitDialog()_function in the lower window to edit it.
- Edit the OnInitDialog() function to add the following code after the line that says
//TODO: Add extra initialization here
:LBase::LoadLibraries(LT_KRN);
LBase::LoadLibraries(LT_DIS);
LBase::LoadLibraries(LT_FIL);
RECT rc;
GetDlgItem(IDC_STATIC)->GetWindowRect(&rc);
ScreenToClient(&rc);
HWND hWnd = m_LBitmapWnd.CreateWnd(this->GetSafeHwnd(),0, WS_VISIBLE|L_BS_CENTER| L_BS_PROCESSKEYBOARD, rc.left, rc.top, rc.right, rc.bottom);
m_LBitmapWnd.EnableAutoScroll(TRUE);
m_LBitmapWnd.SetFileName (MAKE_IMAGE_PATH(TEXT("image1.cmp")));
int nRet = m_LBitmapWnd.Load (0,ORDER_BGR, 1);
HRESULT hr;
CoInitialize(NULL);
hr = m_pConnection.CreateInstance(__uuidof( Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_btnMoveNext.EnableWindow(FALSE);
m_btnMovePrevious.EnableWindow(FALSE);
m_btnAddRecord.EnableWindow(FALSE);
m_btnDeleteRecord.EnableWindow(FALSE);
- Do the following to add an event handler to the Connect To Database button:
- Open ImageBlob.rc and Double-click on IDD_IMAGEBLOB_DIALOG.
- Right-click on the Connect To Database button, and choose Add Event Handler.
- From the Message Type List, choose BN_CLICKED.
- Click the Add and Edit button to close the dialog box, then enter the following code:
HRESULT hr;
_bstr_t bstrConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=d:\\temp\\DB1.mdb;");
_bstr_t bstrQuery("SELECT * FROM Images");
_variant_t vNull;
vNull.vt = VT_ERROR ;
vNull.scode = DISP_E_PARAMNOTFOUND ;
if (m_pConnection->State == adStateClosed)
{
hr = m_pConnection->Open( bstrConnection, _bstr_t(L""), _bstr_t(L""), adModeUnknown ) ;
}
if (SUCCEEDED(hr))
{
m_pRecordset->PutRefActiveConnection(m_pConnection);
hr = m_pRecordset->Open(_variant_t(bstrQuery), vNull, adOpenKeyset, adLockOptimistic, adCmdText );
if (SUCCEEDED(hr))
{
if(!m_pRecordset->GetBOF())
{
m_pRecordset->MoveFirst();
}
if (!m_pRecordset->GetADOEOF())
{
m_btnMoveNext.EnableWindow(TRUE);
m_btnMovePrevious.EnableWindow(TRUE);
m_btnDeleteRecord.EnableWindow(TRUE);
}
m_btnAddRecord.EnableWindow(TRUE);
}
}
18. Do the following to add an event handler to the Disconnect From Data Base button:
- Open ImageBlob.rc and Double-click on IDD_IMAGEBLOB_DIALOG.
- Right-click on the Disconnect From Data Base button, and choose Add Event Handler.
- From the Message Type List, choose BN_CLICKED.
- Click the Add and Edit button to close the dialog box, then enter the following code:
if (m_pConnection->State == adStateOpen)
{
m_pConnection->Close();
}
if (m_pRecordset->State == adStateOpen )
{
m_pRecordset->Close();
}
m_btnMoveNext.EnableWindow(FALSE);
m_btnMovePrevious.EnableWindow(FALSE);
m_btnAddRecord.EnableWindow(FALSE);
m_btnDeleteRecord.EnableWindow(FALSE);
- Do the following to add an event handler to the Add Record button:
- Open ImageBlob.rc and Double-click on IDD_IMAGEBLOB_DIALOG.
- Right-click on the Add Record button, and choose Add Event Handler.
- From Message Type List, choose BN_CLICKED.
- Click the Add and Edit button to close the dialog box, then enter the following code:
L_SIZE_T nSize;
int nRet;
LMemoryFile LeadMemFile;
LBuffer LeadMemBuffer;
if (m_pConnection->State == adStateClosed )
{
MessageBox (TEXT("Connection closed"));
return;
}
LeadMemFile.SetBitmap((LBitmapBase *) &m_LBitmapWnd);
nRet = LeadMemFile.Save(&LeadMemBuffer,FILE_LEAD,24,QS);
nSize = LeadMemBuffer.GetSize();
BYTE * pData = (BYTE *)LeadMemBuffer.Lock();
VARIANT varBLOB;
BlobToVariant(varBLOB, pData, (LONG)nSize);
LeadMemBuffer.Unlock();
LeadMemBuffer.Free();
m_pRecordset->AddNew();
m_pRecordset->Fields->Item[0L]->AppendChunk(varBLOB);
m_pRecordset->Fields->Item[1L]->PutValue((LONG)nSize);
m_pRecordset->Update();
VariantClear(&varBLOB);
if(m_pRecordset->RecordCount >= 0)
{
m_btnMoveNext.EnableWindow(TRUE);
m_btnMovePrevious.EnableWindow(TRUE);
m_btnDeleteRecord.EnableWindow(TRUE);
}
- Do the following to add an event handler to the Delete Record button:
- Open ImageBlob.rc and Double-click on IDD_IMAGEBLOB_DIALOG.
- Right-click on the Delete Record button, and choose Add Event Handler.
- From the Message Type List, choose BN_CLICKED.
- Click the Add and Edit button to close the dialog box, then enter the following code:
if (m_pConnection->State == adStateClosed )
{
MessageBox (TEXT("Connection closed"));
return;
}
if (!m_pRecordset->GetBOF() || !m_pRecordset->GetADOEOF())
{
m_pRecordset->MoveLast();
m_pRecordset->Delete(adAffectCurrent);
m_pRecordset->Update();
if(m_pRecordset->RecordCount <= 0)
{
m_btnMoveNext.EnableWindow(FALSE);
m_btnMovePrevious.EnableWindow(FALSE);
m_btnDeleteRecord.EnableWindow(FALSE);
}
}
21. Do the following to add an event handler to the Move Next button:
- Open ImageBlob.rc and Double-click on IDD_IMAGEBLOB_DIALOG.
- Right-click on the Move Next button, and choose Add Event Handler.
- From the Message Type List, choose BN_CLICKED.
- Click the Add and Edit button to close the dialog box, then enter the following code:
if (m_pConnection->State == adStateClosed )
{
MessageBox (TEXT("Connection closed"));
return;
}
if (!m_pRecordset->GetADOEOF())
{
m_pRecordset->MoveNext();
}
if (m_pRecordset->GetADOEOF())
{
MessageBox(TEXT("Unable to get the Image from db"));
m_pRecordset->MovePrevious();
}
else
{
ReadImage();
}
- Do the following to add an event handler to the Move Previous button:
- Open ImageBlob.rc and Double-click on IDD_IMAGEBLOB_DIALOG.
- Right-click on the Move Previous button, and choose Add Event Handler.
- From the Message Type List, choose BN_CLICKED.
- Click the Add and Edit button to close the dialog box, then enter the following code:
if (m_pConnection->State == adStateClosed )
{
MessageBox (TEXT("Connection closed"));
return;
}
if (!m_pRecordset->GetBOF())
{
m_pRecordset->MovePrevious();
}
if (m_pRecordset->GetBOF())
{
MessageBox(TEXT("Unable to get the Image from db"));
m_pRecordset->MoveNext();
}
else
{
ReadImage();
}
- Go to ImageBlobDlg.h file then add the following function declarations:
afx_msg void OnClose();
void BlobToVariant(VARIANT &varArray, BYTE* pData, LONG lSize);
void ReadImage();
- Go to ImageBlobDlg.cpp file then add the following line between the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP:
- Add the following functions to ImageBlobDlg.cpp:
void CImageBlobDlg::OnClose()
{
if (m_LBitmapWnd.IsAllocated())
{
m_LBitmapWnd.Free();
}
LBase::UnloadLibraries(LT_KRN);
LBase::UnloadLibraries(LT_DIS);
LBase::UnloadLibraries(LT_FIL);
CoInitialize(NULL);
CDialog::OnClose();
}
void CImageBlobDlg::BlobToVariant(VARIANT &varArray, BYTE* pData, LONG lSize)
{
BYTE *pByte;
SAFEARRAY FAR* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = lSize;
// create a single dimensional byte array
psa = SafeArrayCreate(VT_I1, 1, rgsabound);
// set the data of the array with data in the edit box
if(SafeArrayAccessData(psa,(void **)&pByte) == NOERROR)
memcpy((LPVOID)pByte, (LPVOID)pData, lSize);
SafeArrayUnaccessData(psa);
varArray.vt = VT_ARRAY | VT_UI1;
varArray.parray = psa;
}
void CImageBlobDlg::ReadImage()
{
LONG nSize;
BYTE * pData = NULL;
LMemoryFile LeadMemFile;
int nRet;
_variant_t varSize;
varSize = m_pRecordset->Fields->Item[1L]->Value;
nSize = varSize.intVal;
//free old image
m_LBitmapWnd.Free();
MessageBox(TEXT("old image freed"));
if(nSize)
{
_variant_t varBLOB;
VariantInit(&varBLOB);
// get the chunk of data
varBLOB = m_pRecordset->Fields->Item[0L]->GetChunk(nSize);
pData =(BYTE *) malloc(nSize);
memset(pData,0,nSize);
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
BYTE *pBuf = NULL;
SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
LeadMemFile.SetBitmap((LBitmapBase *)&m_LBitmapWnd);
LBuffer LeadMemBuffer (pBuf,nSize);
nRet = LeadMemFile.Load(LeadMemBuffer,24,ORDER_BGR, NULL);
SafeArrayUnaccessData(varBLOB.parray);
}
}
}
26. Create a new file called Imports.cpp in place it beside your project files.
- In the Project Workspace, click the Solution Explorer tab.
- Double-click the ImageBlob folder to open it.
- Right-click the Source files folder and select Add New item.
- Right-click on the Source file.
- Expand Visual C++ tree, if it is not already expanded.
- Select Code from the sub tree.
- Select C++ File (.cpp) from the right window.
- In the name text box, specify Imports.
- Click the OK button.
- Double-click the import.cpp in the solution Explorer and add the following lines(Keep in mind, you may have to change the path to where the Libraries files reside):
#include "StdAfx.h"
#if defined(WIN64)
#pragma comment(lib, "..\\..\\..\\..\\..\\Lib\\CDLL\\x64\\Ltwvc_x.lib")
#else
#pragma comment(lib, "..\\..\\..\\..\\..\\Lib\\CDLL\\Win32\\Ltwvc_u.lib")
#endif // #if defined(WIN64)
27. On the main menu, select Build -> Build ImageBlob.exe to build the project.
28. On the main menu, select Build -> Execute ImageBlob.exe to run the project.
LEADTOOLS Raster Imaging C++ Class Library Help