VC saves and displays database images (JPG and JPEG)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.