Source code: Download
KeyCodeAs follows:
1. Save image data to the database
// Save the BMP image to the database
Try
{
_ Recordsetptr precordset;
Precordset. createinstance (_ uuidof (recordset ));
Precordset-> open ("select * From BMP", _ variant_t (idispatch *) pconnection), adopenstatic, adlockoptimistic, ad1_text );
Precordset-> addnew ();
Precordset-> fields-> item ["bmp id"]-> value = (_ variant_t) m_bmp ID; // BMP ID
Variant pvlist;
Setpicturetovariant (pvlist, (unsigned char *) m_pbmpbuffer );
Precordset-> fields-> item ["BMP image"]-> AppendChunk (pvlist); // BMP Image File
Variantclear (& pvlist );
Precordset-> Update ();
Precordset-> close ();
Afxmessagebox ("BMP image saved successfully! ");
}
Catch (...)
{
Afxmessagebox ("database read failed ");
Return;
}
Here, setpicturetovariant is as follows:
Void cbmpindbdlg: setpicturetovariant (variant & pvlist, unsigned char * spicture)
{
Safearraybound sabound [1];
Sabound [0]. celements = m_nfilelen;
Sabound [0]. llbound = 0;
Safearray * PSA = safearraycreate (vt_ui1, 1, sabound );
For (long l = 0; L <(long) m_nfilelen; l ++)
{
Safearrayputelement (PSA, & L, (void *) & spicture [l]);
}
Variantclear (& pvlist );
Pvlist. Vt = vt_ui1 | vt_array;
Pvlist. parray = psa;
}
2. Read image files from the database and display them on the Interface
// Retrieve the BMP image file from the database
Try
{
_ Recordsetptr precordset;
Char ssql [129];
Sprintf (ssql, "select * From BMP where BMP id = '% S'", m_bmp ID );
Precordset. createinstance (_ uuidof (recordset ));
Precordset-> open (ssql, _ variant_t (idispatch *) pconnection), adopenstatic, adlockoptimistic, ad1_text );
If (precordset-> adoeof)
{
Cstring STR;
Str. Format ("No BMP image with the bmp id: % s! ", M_bmp ID );
Afxmessagebox (STR );
Destroyphoto ();
Invalidate ();
M_bmp id = "";
Updatedata (false );
M_eidtbmpid.setfocus ();
Return;
}
_ Variant_t pvlist;
Long ldatasize = precordset-> getfields ()-> getitem ("BMP image")-> actualsize;
M_nfilelen = (DWORD) ldatasize;
If (ldatasize> 0)
{
_ Variant_t varblob;
Varblob = precordset-> getfields ()-> getitem ("BMP image")-> getchunk (ldatasize );
// Convert an image in binary format to an image format
If (varblob. Vt = (vt_array | vt_ui1 ))
{
If (m_pbmpbuffer = new char [ldatasize + 1])
{
Char * pbuf = NULL;
Safearrayaccessdata (varblob. parray, (void **) & pbuf );
Memcpy (m_pbmpbuffer, pbuf, ldatasize );
Safearrayunaccessdata (varblob. parray );
M_nfilelen = ldatasize;
M_hbitmap = buffertohbitmap ();
Invalidate (); // displayed on the page
}
}
}
}
Catch (...)
{
Afxmessagebox ("database read failed ");
Return;
}
Last time I wrote two articles about displaying images (BMP and jpg) on the VC interfaceArticleBut the database is not involved, so this time I wrote these two articles about the database. These two articles use the ACCESS database, and the operations of other databases are the same.
SpecificProgramFor more information, see the attached example.