There seems to be no such example on the Internet. If you use VC to build a human resources department management system, it is impossible to have no photos of people! BMP is used in the examples that can be found!
In this example, vc6.0 and access2002 (officexp) are used, involving the usage of ADO, the usage of the file dialog box, a cpicture class, and a buffer zone.
I. sorting out my ADO usage
1. Add the following to the stdafx. h header file:
#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF")
2. Add the following to application initialization:
Afxoleinit (); hresult hr; try {hR = m_pconnection.createinstance ("ADODB. connection "); // create a connection object if (succeeded (HR) {hR = m_pconnection-> open (" provider = Microsoft. jet. oledb.4.0; Data Source = WY. mdb "," "," ", admodeunknown); // connect to the database // The provider in the connection string in the above sentence is for the Access2000 environment. For Access97, you need to change it: provider = Microsoft. jet. oledb.3.51 ;}} catch (_ com_error e) // catch exception {cstring errormessage; errormessage. format ("Open WY. mdb database failed! /R/n error message: % s ", E. errormessage (); afxmessagebox (errormessage); // Display error message}
3. Add the following to application Exit:
if (m_pConnection->State) m_pConnection->Close();
4. Add the following to the application:
_ConnectionPtr m_pConnection;
5. Add the following to the dialog box:
_RecordsetPtr m_pRecordset;
6. Note the following:
extern CWYApp theApp;
7. Use record set:
Try {m_precordset.createinstance ("ADODB. recordset "); m_precordset-> open (" select * from customer order by Customer ID ", _ variant_t (idispatch *) theapp. m_pconnection, true), adopenstatic, adlockoptimistic, adshorttext);} catch (_ com_error e) // catch an exception {afxmessagebox ("failed to read the database! "); // Display Error information}
Cpicture class (it can display jpg. gif and other images. For details, see the cpicture. h header file)
Cpicture. h
Cpicture. cpp
Ii. Flowchart
You can see that ADO is used between the buffer memory and access2002 database, while cpicture is used between the buffer memory and display window.
3. Save the JPG file into the database and display it
Void conedlg: onbutton1 () {cfile F; cstring filepathname; cfileexception E; cfiledialog DLG (true, null, null, 0, "JPG files (*. JPG) | *. JPG | ", this); If (DLG. domodal () = idok) {filepathname = DLG. getpathname (); If (m_pic.m_ipicture! = NULL) m_pic.freepicturedata (); // important-avoid leaks... if (F. open (filepathname, cfile: moderead | cfile: typebinary, & E) // open a JPG file {int nsize = f. getlength (); // first obtain the JPG file Length Byte * pbuffer = new byte [nsize]; // apply for a memory on the heap based on the file size if (F. read (pbuffer, nsize)> 0) // read the JPG file to pbuffer (applying for a block of memory on the stack) {byte * pbuf = pbuffer; /// the main section below is to put the JPG data in pbuffer into the database variant varblob; safearray * PSA; safearraybound rgsabound [1]; m_precordset-> addnew (); if (pbuf) {rgsabound [0]. llbound = 0; rgsabound [0]. celements = nsize; PSA = safearraycreate (vt_ui1, 1, rgsabound); For (long I = 0; I <(long) nsize; I ++) safearrayputelement (PSA, & I, pbuf ++); varblob. vt = vt_array | vt_ui1; varblob. parray = psa; m_precordset-> getfields ()-> getitem ("J")-> AppendChunk (varblob);} m_precordset-> Update (); (m_pic.loadpicturedata (pbuffer, nsize); // call the function to read the JPG data of pbuffer and prepare to display Delete [] pbuffer; // Delete the memory pbuf applied for on the stack = 0; // prevent second misuse} f. close () ;}cclientdc DC (this); convert (& DC); // get picture dimentions in pixelsm_pic.show (& DC, crect (200, + m_pic.m_width, m_pic.m_height )); // show it }}.......
The complete example contains many Chinese descriptions. Is it useful to you !?, Please advise me what's wrong