[Reprint] Using stream object in vc6.0 to read data files and display BMP, JPG, etc.

Source: Internet
Author: User

In vc6.0, we usually use getchuck and AppendChunk of the Field object of ADO to read and write blob objects.CodeIn fact, ADO provides us with an object that is easier to operate, namely Stream object, through which we can more easily operate blob objects in the database, in addition, blob objects can be directly saved from the database to a local file, or files can be directly read and written to the database.

The following describes in detail how to perform the operation.

_ Streamptr PSTM; // Declare Stream Object
PSTM. createinstance ( " ADODB. Stream " );
Variant_t varoptional (disp_e_paramnotfound, vt_error );
PSTM -> Puttype (adtypebinary ); // Binary type
// Long ldatasize = m_precordset-> getfields ()-> getitem ("photo")-> actualsize; // Obtain the size of the field content.
// M_precordset is an open record set object that contains the Blob field photo.
PSTM -> Open (varoptional, admodeunknown, adopenstreamunspecified, _ bstr_t (), _ bstr_t ()); // Open PSTM
PSTM -> Write (_ variant_t (m_precordset -> Getfields () -> Getitem ( " Photo " ) -> Value )); // Write the content of the photo field to PSTM.
PSTM -> Savetofile ( " C: \ publogo.jpg " , Adsavecreateoverwrite );
PSTM -> Close ();

Save it as a file directly. If it is another format, you only need to change the suffix when saving it.

If you do not save the image as a file, you can use the COM object to draw the image. ipicture can display multiple formats of images.

Hglobal = Globalalloc (gmem_moveable, PSTM -> Getsize ());
Lpvoid pvdata = NULL;
Istream * PS;
If (Hglobal ! = Null)
{
If (Pvdata = Globallock (hglobal )) ! = Null)
{
Char   * M_pbmpbuffer =   New   Char [PSTM -> Getsize () + 1 ]; // Allocate necessary storage space
Char   * Pbuf = NULL;
Safearrayaccessdata (varblob. parray ,( Void   ** ) & Pbuf );
Memcpy (pvdata, pbuf, PSTM -> Getsize ()); // Copy data to the buffer area m_pbmpbuffer
Safearrayunaccessdata (varblob. parray );
Globalunlock (hglobal );
Createstreamonhglobal (hglobal, true, & PS );
}
}

Ipicture * PPIC;
If (Succeeded (oleloadpicture (Ps, PSTM -> Getsize (), true, iid_ipicture, (lpvoid * ) & PPIC )))
{
Ole_xsize_himetric hmwidth;
Ole_ysize_himetric hmheight;
PPIC -> Get_width ( & Hmwidth );
PPIC -> Get_height ( & Hmheight );
Double FX, FY;
CDC * PDC = Getclientdc (); // Here we will retrieve the data based on actual conditions.
FX = ( Double ) PDC -> Getdevicecaps (horzres) * ( Double ) Hmwidth / (( Double ) PDC -> Getdevicecaps (horzsize) * 100.0 );
FY = ( Double ) PDC -> Getdevicecaps (vertres) * ( Double ) Hmheight / (( Double ) PDC -> Getdevicecaps (vertsize) * 100.0 );
If (Failed (PPIC -> Render ( * PDC, 0 , 0 , (DWORD) FX, (DWORD) FY, 0 , Hmheight, hmwidth, - Hmheight, null )))
Afxmessagebox ( " Image painting failed! " );
PPIC -> Release ();
}

 
This completes the process of reading and displaying image files from the database.
It is easier to write data from files to the database,

_ Streamptr PSTM;
PSTM. createinstance ( " ADODB. Stream " );
Variant_t varoptional (disp_e_paramnotfound, vt_error );
// Long ldatasize = m_precordset-> getfields ()-> getitem ("photo")-> actualsize;
PSTM -> Puttype (adtypebinary;

PSTM -> Open (varoptional, admodeunknown, adopenstreamunspecified, _ bstr_t (), _ bstr_t ());
PSTM -> Loadfromfile ( " C: \ book.gif " ); // Read files
Variant_t varblob = PSTM -> Read (adreadall );
M_precordset -> Getfields () -> Getitem ( " Photo " ) -> Value = Varblob; // Save to the DataSet object.

The above is the specific method for operating BLOB fields in the database by the stream object of ADO. We can also convert images into other methods. For example, readers can convert images into XML files, and save.

Due to my limited level, there are inevitably many shortcomings in writing. Please kindly advise and hope to send a letter to us. For example code or exchange please send to arvid_gs@163.com, or in the csdn forum to arvid_gs short message.

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.