Use Stream Object to read data in the data and display BMP, JPG, etc. in VC6.0.

zhaozj2021-02-17  50

In VC6.0, we usually read the blob object with the getchuck and appendchunk of the ADO's field object, but doing this to write a lot of code, actually ADO provides us with a more easy way to operate, the Stream Object, through us You can make it easier to operate the BLOB object in the database, and you can directly save the BLOB object from the database to the local file, or directly read the file to the database. How to operate in detail below.

First Stream object stated _ StreamPtr pStm; pStm.CreateInstance ( "ADODB.Stream"); ​​variant_t varOptional (DISP_E_PARAMNOTFOUND, VT_ERROR); pStm-> PutType (adTypeBinary); // // obtained binary type field contents // Long Size lDataSize = m_pRecordset-> GetFields () -> GetItem ( "photo") -> ActualSize; // m_pRecordset record set to an open object, comprising the photo blob field pStm-> open (varOptional, adModeUnknown, adOpenStreamUnspecified, _bstr_t () , _BSTR_T ()); // Open PSTM PSTM-> Write (_variant_t (m_precordset-> getfields () -> GetItem ("photo") -> value)); // Write the contents of the photo field into PSTM-> SaveTofile ("c: //publogo.jpg", adsavecreateoverwrite; pstm-> close (); // Save as file, if you have changed the suffix as long as you save, you can do it if you do not save into a file This image can be drawn directly through the COM object, iPicture can display a variety of format pictures. Hglobal Hglobal = GlobalAlloc (GMEM_MOVEABLE, PSTM-> GetSize ()); lpvoid pvdata = null; istream * ps; if (hglobal! = Null) { IF ((pvdata = Globalock) (HGLO BAL)! = null) {char * m_pbmpbuffer = new char [PSTM-> getSize () 1]; // Assign the necessary storage space char * PBUF = null; SafearrayAccessData (Varblob.Parray, (void **) & PBUF ); Memcpy (PVDATA, PBUF, PSTM-> getSize ()); // Copy data to buffer 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 (); // This here, 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 Drawing Failed!"); PPIC-> Release ();} The process of reading and displaying the image file from the database is completed. For more than easy to write the database from the file, _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 file Variant_t varblob = PSTM-> Read (Adreadall ); M_precordset-> getfields () -> GetItem ("photo") -> value = varblob; // Save to Data Set Object. The above is the specific method of the BLOB field in the Stream object in the ADO, mainly for the picture, and we can also transform it into other, such as readers can transform into read XML files, and save, etc. Since I have limited level, there is a lot of deficiencies, please ask you to advice, and hope to communicate. Request example code or exchange, please let Arvid_Gs@163.com, or send a short message to the ARVID_GS in the 9CBS forum.

转载请注明原文地址:https://www.9cbs.com/read-30257.html

New Post(0)