Source code: Download
KeyCodeAs follows:
1. Save image data to the database
// Save the jpg image to the database
Try
{
_ Recordsetptr precordset;
Precordset. createinstance (_ uuidof (recordset ));
Precordset-> open ("select * From jpg", _ variant_t (idispatch *) pconnection), adopenstatic, adlockoptimistic, ad1_text );
Precordset-> addnew ();
Precordset-> fields-> item ["jpgid"]-> value = (_ variant_t) m_jpgid; // jpgid
Variant pvlist;
Setpicturetovariant (pvlist, (unsigned char *) m_pjpgbuffer );
Precordset-> fields-> item ["jpgimage"]-> AppendChunk (pvlist); // jpg image file
Variantclear (& pvlist );
Precordset-> Update ();
Precordset-> close ();
Afxmessagebox ("jpg image saved successfully! ");
M_jpgid = "";
Updatedata (false );
}
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
// Obtain jpg image files from the database
Try
{
_ Recordsetptr precordset;
Char ssql [129];
Sprintf (ssql, "select * From JPG where jpgid = '% S'", m_jpgid );
Precordset. createinstance (_ uuidof (recordset ));
Precordset-> open (ssql, _ variant_t (idispatch *) pconnection), adopenstatic, adlockoptimistic, ad1_text );
If (precordset-> adoeof)
{
Cstring STR;
Str. Format ("No jpg image with jpgid: % s! ", M_jpgid );
Afxmessagebox (STR );
Invalidate ();
M_jpgid = "";
Updatedata (false );
M_eidtjpgid.setfocus ();
Return;
}
_ Variant_t pvlist;
Long ldatasize = precordset-> getfields ()-> getitem ("jpgimage")-> actualsize;
M_nfilelen = (DWORD) ldatasize;
If (ldatasize> 0)
{
_ Variant_t varblob;
Varblob = precordset-> getfields ()-> getitem ("jpgimage")-> getchunk (ldatasize );
// Convert an image in binary format to an image format
Try
{
If (varblob. Vt = (vt_array | vt_ui1 ))
{
If (m_pjpgbuffer = new char [ldatasize + 1])
{
Char * pbuf = NULL;
Safearrayaccessdata (varblob. parray, (void **) & pbuf );
Memcpy (m_pjpgbuffer, pbuf, ldatasize );
Safearrayunaccessdata (varblob. parray );
M_nfilelen = ldatasize;
// M_pjpgbuffer-> ppicture
Hglobal hmem =: globalalloc (gmem_moveable, m_nfilelen );
Lpvoid lpbuf =: globallock (hmem );
Memcpy (lpbuf, m_pjpgbuffer, m_nfilelen );
: Globalunlock (hmem );
If (createstreamonhglobal (hmem, true, & pstream )! = S_ OK)
Return;
If (oleloadpicture (pstream, m_nfilelen, true, iid_ipicture, (lpvoid *) & ppicture )! = S_ OK)
Return;
Invalidate (); // displayed on the page
}
}
}
Catch (...)
{
Afxmessagebox ("An error occurred while reading JPG images from the database! ");
Return;
}
}
}
Catch (...)
{
Afxmessagebox ("database read failed ");
Return;
}
SpecificProgramFor more information, see the attached example.