Putting and retrieving image files in to a database, is not a simple task. I you want to look on the Internet for such a sample, you will be surprised that there is no C++ sample for that. There is only information, that you have to use the
This article will show how you can put image files in to a database via ADO, as well as how you can retrieve them. With this example, you can use any file format such as Word, Excel etc., not just image file formats.
The example contains two methods for putting and retrieving image files to and from a database. The first method retrieves data from the database, create a file in a temporary directory, and puts the data into a file created. The parameter
strImageName is the name of the file that will be created. The second parameter indicates the ADO field object containing the image data.
The following code sample shows the implementation:
strImageName, FieldPtr pField)
unsigned char* lpData = NULL;
long lngOffSet = 0;
const long ChunkSize=50;
long lBytesCopied = 0;
lpData=new unsigned char [lngSize];
while(lngOffSet < lngSize)
varChunk = pField->GetChunk(ChunkSize);
for(long lIndex = 0; lIndex <=
(ChunkSize - 1); lIndex++)
((UCHAR*)lpData)[lBytesCopied] = chData;
lngOffSet += ChunkSize;
LPSTR buffer = (LPSTR)GlobalLock(lpData);
ULONG nBytes = (ULONG)fileStatus.m_size;
HGLOBAL hGlobal = GlobalAlloc(GPTR,nBytes);
LPVOID lpData = GlobalLock(hGlobal);
long lngOffset = 0;
SAFEARRAY FAR *psa = NULL;
rgsabound.lLbound = 0;
rgsabound.cElements = nBytes;
psa = SafeArrayCreate(VT_UI1,1,rgsabound);
while(lngOffset < (long)nBytes)
chData = ((UCHAR*)lpData)[lngOffset];
hr = SafeArrayPutElement(psa,
lngOffset = 0;
varChunk.vt = VT_ARRAY|VT_UI1;
varChunk.parray = psa;
hr = pFileData->AppendChunk(varChunk);
For image display, I created a class which wraps the freeimage library.
- 22.214.171.124 - January 27, 2006 - First release version.
Bahrudin Hrnjica holds a Ph.D. degree in Technical Science/Engineering from University in Bihać.
Besides teaching at University, he is in the software industry for more than two decades, focusing on development technologies e.g. .NET, Visual Studio, Desktop/Web/Cloud solutions.
He works on the development and application of different ML algorithms. In the development of ML-oriented solutions and modeling, he has more than 10 years of experience. His field of interest is also the development of predictive models with the ML.NET and Keras, but also actively develop two ML-based .NET open source projects: GPdotNET-genetic programming tool and ANNdotNET - deep learning tool on .NET platform. He works in multidisciplinary teams with the mission of optimizing and selecting the ML algorithms to build ML models.
He is the author of several books, and many online articles, writes a blog at http://bhrnjica.net, regularly holds lectures at local and regional conferences, User groups and Code Camp gatherings, and is also the founder of the Bihac Developer Meetup Group. Microsoft recognizes his work and awarded him with the prestigious Microsoft MVP title for the first time in 2011, which he still holds today.