VC saves and displays database images (BMP)

Source: Internet
Author: User
Tags bmp image

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.

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.