VC uses ADO to access and display jpg/BMP bitmap files

Source: Internet
Author: User

On Saturday and today, the storage and display of images in the database were realized. Although the time was a little long, it was still realized. The following documents are useful.

Http://hi.baidu.com/%B0%A2%B3%ACyuch/blog/item/3a0d0845980f0744500ffe5b.html

Step 1: First, open a bitmap file. The controls used here use the picture control, which is the one on the right of the control icon. Change the ID number to idc_picture and define two member variables.
Char * m_char; // image file pointer
DWORD m_nfilelen; // image Length
Then write in the function:
Cfiledialog DLG (true, null, null, 0, "photo files (*. jpg; *. BMP) | *. jpg; *. BMP |", this );
If (idok = DLG. domodal ())
{
M_path = DLG. getpathname ();
M_bool = true;
}
Cwnd * pwnd = getdlgitem (idc_picture );
Crect rect;
Pwnd-> getclientrect (& rect );
CDC * PDC = pwnd-> getdc ();
Cfilestatus fstatus;
Cfile file;
Long CB;
Bool m_tm = false;
Ipicture * PPIC;
Cstring m_spath;
If (file. Open (m_path, cfile: moderead) & file. getstatus (m_path, fstatus) & (cb = fstatus. m_size )! =-1 ))

{

If (CB> 1048576) // you can set the image size here
{
M_tm = false;
MessageBox ("image cannot exceed 1 MB", "prompt! ");
}
Else
{

M_nfilelen = (uint) Cb;
Hglobal = globalalloc (gmem_moveable, CB );
Lpvoid pvdata = NULL;
If (hglobal! = NULL)
{
If (pvdata = globallock (hglobal ))! = NULL)
{
File. readhuge (pvdata, CB );
M_char = (char *) pvdata;
Globalunlock (hglobal );
Createstreamonhglobal (hglobal, true, & PSTM );
M_tm = true;
// M_bool = true;
}
Else
Afxmessagebox ("not an image file! ");
}
Else
Afxmessagebox ("memory application failed! ");

}
}
Else
Afxmessagebox ("not an image file! ");
If (m_tm)
{
Succeeded (oleloadpicture (PSTM, fstatus. m_size, true, iid_ipicture, (lpvoid *) & PPIC ));
Ole_xsize_himetric hmwidth;
Ole_ysize_himetric hmheight;
PPIC-> get_width (& hmwidth );
PPIC-> get_height (& hmheight );
If (failed (PPIC-> render (* PDC, 0, 0, rect. Width (), rect. Height (), 0, hmheight, hmwidth,-hmheight, null )))

Afxmessagebox ("rendering image failed! ");
PPIC-> release ();
}
There is also a small problem here, that is, when the window is re-painted, the image will disappear, you can add the above Code to the onpaint function;
The connection is to save the file (for how to access the database, you can see "access the database with ADO" in my space "):
Change the type of the photo field to Ole type in the Access Database (change to the photo type in the SQL database ).
M_recordset-> addnew ();
Char * pbuf = m_char; // pass the pointer of the image to pbuf.
Variant varblob;
Safearray * PSA;
Safearraybound rgsabound [1];
If (pbuf)
{
Rgsabound [0]. llbound = 0;
Rgsabound [0]. celements = m_nfilelen;
PSA = safearraycreate (vt_ui1, 1, rgsabound );
For (long I = 0; I <(long) m_nfilelen; I ++)
Safearrayputelement (PSA, & I, pbuf ++ );
Varblob. Vt = vt_array | vt_ui1;
Varblob. parray = psa;
M_precordset-> getfields ()-> getitem ("")-> AppendChunk (varblob );
}
M_precordset-> Update ();

The next step is to extract the image from the database, add the picture control in the displayed dialog box, change the ID to idc_picture, and then write it in the function:
Istream * PSTM;
Long ldatasize = m_precordset-> getfields ()-> getitem ("")-> actualsize;
If (ldatasize> 0)
{
_ Variant_t varblob;
Varblob = theapp. m_data.m_precordset-> getfields ()-> getitem ("")-> getchunk (ldatasize );

If (varblob. Vt = (vt_array | vt_ui1 ))
{
Char * pbuf = NULL;
Safearrayaccessdata (varblob. parray, (void **) & pbuf );
Hglobal = globalalloc (gmem_moveable, ldatasize );
Lpvoid pvdata = NULL;
If (hglobal! = NULL)
{
If (pvdata = globallock (hglobal ))! = NULL)
{
Memcpy (pvdata, pbuf, ldatasize );
Safearrayunaccessdata (varblob. parray );
Globalunlock (hglobal );
Createstreamonhglobal (hglobal, true, & PSTM );
}
Else
Afxmessagebox ("An error occurred while loading the image! ");
}
Else
Afxmessagebox ("memory application failed! ");

}
Cwnd * pwnd = getdlgitem (idc_picture );
Crect rect;
Pwnd-> getclientrect (& rect );
CDC * PDC = pwnd-> getdc ();
Ipicture * PPIC;
If (succeeded (oleloadpicture (PSTM, ldatasize, true, iid_ipicture, (lpvoid *) & PPIC )))

{
Ole_xsize_himetric hmwidth;
Ole_ysize_himetric hmheight;
PPIC-> get_width (& hmwidth );
PPIC-> get_height (& hmheight );
If (failed (PPIC-> render (* PDC, 0, 0, rect. Width (), rect. Height (), 0, hmheight, hmwidth,-hmheight, null )))

Afxmessagebox ("rendering image failed! ");
PPIC-> release ();
}
}
This will also cause the problem that the image disappears when the window is re-painted. You can write the above function in onpaint. the entire display, accessing and accessing images is complete.

Http://hi.baidu.com/roufei13/blog/item/6528919e0fb23aa4c9eaf4d8.html

Coinitialize (null );

 

_ Connectionptr m_pconnect;

Try

{

// Create a connection object

M_pconnect.createinstance ("ADODB. Connection ");

// Set the connection string, which must be of the BSTR or _ bstr_t type

_ Bstr_t strconnect = "provider = sqloledb.1; Password = 111111; persist Security info = true; user id = sa; initial catalog = picture; Data Source = showfly \ sql2005 ";

M_pconnect-> open (strconnect, "", "", admodeunknown );

}

// Catch exceptions

Catch (_ com_error E)

{

// Display the error message

Afxmessagebox (E. Description ());

}

 

Cfile fileadd;

 

If (fileadd. Open ("F:/20087610203.jpg", cfile: moderead) = 0) // open the file

Return;

 

_ Variant_t varchunk;

 

Long m_nfilelen = fileadd. getlength ();

Byte * m_pbmpbuffer;

M_pbmpbuffer = new byte [m_nfilelen];

 

If (m_pbmpbuffer = NULL)

Return;

Fileadd. Read (m_pbmpbuffer, m_nfilelen );

 

// Add images to the database

_ Recordsetptr m_precordset;

M_precordset.createinstance (_ uuidof (recordset ));

 

Try {

M_precordset-> open (_ variant_t ("DBO. userphoto"), _ variant_t (idispatch *) m_pconnect, true), adopenkeyset, adlockoptimistic, adcmdtable );

}

Catch (_ com_error & E)

{

: MessageBox (null, "The userphoto table cannot be opened! "," Prompt ", mb_ OK | mb_iconwarning );

}

 

Char * pbuf = (char *) m_pbmpbuffer;

Variant varblob;

Safearray * PSA;

Safearraybound rgsabound [1];

 

M_precordset-> addnew (); // Add a new record

M_precordset-> putcollect ("username", _ variant_t ("Xiao Li"); // fill in the username field for the new record

M_precordset-> putcollect ("old", _ variant_t (long) 28); // fill in the old field

If (pbuf)

{

Rgsabound [0]. llbound = 0;

Rgsabound [0]. celements = m_nfilelen;

PSA = safearraycreate (vt_ui1, 1, rgsabound); // create a safearray object

For (long I = 0; I <(long) m_nfilelen; I ++)

Safearrayputelement (PSA, & I, pbuf ++); // Save the binary data that pbuf points to the safearray object PSA

Varblob. Vt = vt_array | vt_ui1; // sets the varblob type to an array of the byte type.

Varblob. parray = psa; // assign a value to the varblob variable

M_precordset-> getfields ()-> getitem ("photo")-> AppendChunk (varblob); // Add BLOB Data

}

M_precordset-> Update (); // save our data to the database

 

 

// Read the image from the database

Long ldatalength = m_precordset-> getfields ()-> getitem (_ variant_t ("photo")-> actualsize;

 

If (ldatalength> 0)

{

_ Variant_t varblob;

Varblob = m_precordset-> getfields ()-> getitem (_ variant_t ("photo")-> getchunk (ldatalength );

 

If (varblob. Vt = (vt_array | vt_ui1) & varblob. VT! = Vt_empty & varblob. VT! = Vt_null)

{

Byte * pbuf = NULL;

Pbuf = (byte *) globalalloc (gmem_fixed, ldatalength );

 

Safearrayaccessdata (varblob. parray, (void **) & pbuf );

 

Cfile OUTFILE ("D:/20087610203.bmp", cfile: modecreate | cfile: modewrite); // create a new file. If the file exists, the length is changed to 0.

OUTFILE. Write (pbuf, ldatalength );

OUTFILE. Close ();

 

Safearrayunaccessdata (varblob. parray );

}

}

M_precordset-> close ();

 

M_pconnect-> close ();

 

: Couninitialize ();

Http://www.cnblogs.com/-clq/archive/2011/08/27/2155342.html

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.