Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and display image data in Visual C++

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to read and display image data in Visual C++. The content is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

When programming the database with VC, it is often necessary to deal with the image data in the database, read the image from the database and display it. Different from the text field, the image data is stored in the database as an OLE field. The image data is automatically exchanged through the member variables of the dataset object, and the obtained data can not be displayed directly. How to deal with the image data? It has always been a difficult point in database programming.

At present, there is a lot of information about VC database programming, but it rarely involves the operation of image data. In view of the current situation, combined with a project developed by myself, the author solves the problem of how to display images in the database, and takes the operation of ACESS database as an example to explain his own implementation ideas.

To simplify the problem, there is only one OLE field named Images in the table of the database. I use DAO connection to operate the database, and the image data I read is displayed on a dialog box. As for using ODBC, DAO, or ADO, it depends on the situation, but no matter which one is used, the process of image display is more or less the same.

Due to the limited space, the article no longer makes a specific explanation on how to connect to the database. Interested readers can refer to the materials of VC database programming. In the process of implementation, first define a subclass CimageData of CDaoRecordset as follows:

Class CimageData: public CDaoRecordset {public: CimageData (CDaoDatabase* pDatabase = NULL); DECLARE_DYNAMIC (CimageData) file://{{AFX_FIELD (CimageData, CDaoRecordset) CByteArray masquerade / declaration byte array used to store image data file://}}AFX_FIELD / / Overrides / / ClassWizard generated virtual function overrides file://{{AFX_VIRTUAL (CimageData) public: virtual CString GetDefaultDBName (); virtual CString GetDefaultSQL (); virtual void DoFieldExchange (CDaoFieldExchange* pFX) File://}}AFX_VIRTUAL

The implementation of this class is:

CimageData:: CimageData (CDaoDatabase* pdb): CDaoRecordset (pdb) {file://{{AFX_FIELD_INIT (CimageData) m_nFields = 1 there is only one field in the table of the database file://}}AFX_FIELD_INIT m_nDefaultType = dbOpenDynaset;// to open the database in dynamic set mode} CString CimageData::GetDefaultDBName () {return _ T ("E:\\ IMAGES.mdb") / / default ACESS database on E disk, named IMAGES} CString CimageData::GetDefaultSQL () {return _ T ("[Table]"); / / Open the table named "Table" in the database} void CimageData::DoFieldExchange (CDaoFieldExchange* pFX) {file://{{AFX_FIELD_MAP (CimageData) pFX- > SetFieldType (CDaoFieldExchange::outputColumn) by default; DFX_Binary (pFX, _ T ("[Images]"), m_Images) / / Exchange data between Images fields and m_Images variables in binary file://}}AFX_FIELD_MAP}

With this class, you can define the corresponding object to exchange data with the image field in the database. The function GetImageData () defined below shows how to generate the image to be displayed based on the read OLE field data. Note that the variable Bitmap of the CBitmap class used in this function is a predefined global variable:

BOOL CImageDlg:: GetImageData (CByteArray & DBArray) {CByteArray Array; Array.Copy (DBArray); int HeaderLen = 78 + sizeof (BITMAPFILEHEADER); file:// determines the starting position of the image header information Array.RemoveAt (0, HeaderLen); / / moves to the starting position of the image header information BITMAPINFOHEADER & bmiHeader = * (LPBITMAPINFOHEADER) Array.GetData (); BITMAPINFO & bmInfo = * (LPBITMAPINFO) Array.GetData (); file:// gets the image data header information int nColors=bmiHeader.biClrUsed? BmiHeader.biClrUsed: 1 "bmiHeader.biBitCount; file:// determines the color number of an image LPVOID lpDIBBits; if (bmInfo.bmiHeader.biBitCount > 8) lpDIBBits= (LPVOID) ((LPDWORD) (bmInfo.bmiColors+bmInfo.bmiHeader.biClrUsed) + ((bmInfo.bmiHeader.biCompression = = BI_BITFIELDS)? 3: 0)); else lpDIBBits= (LPVOID) (bmInfo.bmiColors+ nColors); file:// obtains the specific data CClientDC dc (NULL) of each pixel of the image. HBITMAP hBmp = CreateDIBitmap (dc.m_hDC, & bmiHeader, CBM_INIT, lpDIBBits, & bmInfo, DIB_RGB_COLORS); file:// generates bitmap handle Bitmap.Attach (hBmp); / / associates the handle with the defined Bitmap object Array.RemoveAll (); file:// frees memory return TRUE;}

With the above preparation, you can now implement the image display function, which is as follows:

Void CImageDlg::OnShowImage () {CimageData db;// defines the recordset object db.Open (); opens the database GetImageData (db.m_Images); / / generates an image object based on the member variables of the recordset object file:// below is the display image CPaintDC dc (this) in the fixed area of the dialog box; if (! (Bitmap.m_hObject = = NULL) {CDC dcMem; dcMem.CreateCompatibleDC (& dc); file://create a Memory Image CBitmap* pbmpOld; BITMAP BmpSize; Bitmap.GetBitmap (& BmpSize); file://get Image Size pbmpOld = dcMem.SelectObject (& Bitmap); dc.StretchBlt (20,20,200,200, & dcMem, 0,0, BmpSize.bmWidth, BmpSize.bmHeight, SRCCOPY); dcMem.SelectObject (pbmpOld);}

The database used in the above code is ACESS97, and the program is compiled under the environment of windows98 and Visual Cellular 6.0, and runs normally.

The above content is how to read and display image data in Visual C++. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report