Reading and displaying image data in Visual C ++

Source: Internet
Author: User
When using VC for database programming, it is often necessary to process the image data in the database and read and display the image from the database. The image data is different from the text field, it is stored as an OLE field in the database. image data is automatically exchanged through the member variables of the DataSet object, and the obtained data cannot be directly displayed. How to process image data, it has always been a difficult point in database programming. At present, there are a lot of materials on VC for database programming, but few operations on image data are involved. In view of the current situation, I developed a project based on my own, the problem of how to display images in the database is solved. This article uses the ACESS database as an example to explain its implementation ideas and hope to help those who are interested in VC programming, in order to play a role.

To simplify the problem, there is only one OLE field named Images in the table in the database. I connected to the database using DAO, and the image data read was displayed in a dialog box, the use of ODBC, DAO, and ADO depends on the specific situation. However, no matter which one is used, the implementation process for image display is similar. Due to the limited length of the article, the article will not detail how to achieve database connection. Interested readers can refer to VC database programming materials. During implementation, CimageData, a subclass of CDaoRecordset, is defined as follows:

Class CimageData: public CDaoRecordset
{
Public:
CimageData (CDaoDatabase * pDatabase = NULL );
DECLARE_DYNAMIC (CimageData)
File: // {AFX_FIELD (CimageData, CDaoRecordset)
CByteArray m_Images; // declare a byte array to store image data
File: //} AFX_FIELD
// Overrides
// ClassWizard generated virtual function overrides
File: // {AFX_VIRTUAL (CimageData)
Public:
Virtual CString getdefadbdbname ();
Virtual CString getdefasql SQL ();
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; // The database table has only one Field
File: //} AFX_FIELD_INIT
M_nDefaultType = dbOpenDynaset; // open the database in a dynamic set mode
}
CString CimageData: GetDefaultDBName ()
{
Return _ T ("E: // IMAGES. mdb"); // The default ACESS database is on the E disk named IMAGES
}

Cstring cimagedata: getdefasql SQL ()
{
Return _ T ("[Table]"); // The table named "table" in the database is opened by default.
}

Void cimagedata: dofieldexchange (cdaofieldexchange * pfx)
{
File: // {afx_field_map (cimagedata)
Pfx-> setfieldtype (cdaofieldexchange: outputcolumn );
Dfx_binary (pfx, _ T ("[images]"), m_images); // exchanges data between the images field and the m_images variable in binary mode
File: //} afx_field_map
}
 

With this class, you can define the corresponding object to exchange data with the image fields in the database. The following defined function getimagedata () illustrates how to generate the image to be displayed based on the read Ole field data, note that bitmap is a predefined global variable for the cbitmap class used in this function:

Bool cimagedlg: getimagedata (cbytearray & dbarray)
{
Cbytearray array;
Array. Copy (dbarray );
Int headerlen = 78 + sizeof (bitmapfileheader); file: // determine the starting position of the image header information
Array. removeat (0, headerlen); // move to the starting position of the image header information
Bitmapinfoheader & bmiheader = * (lpbitmapinfoheader) array. getdata ();
Bitmapinfo & bminfo = * (lpbitmapinfo) array. getdata ();
File: // get the header information of the image data
Int ncolors = bmiheader. biclrused? Bmiheader. biclrused: 1 <bmiheader. bibitcount;
File: // determine the number of colors of the 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: // obtain the specific data of each pixel of the image.
CClientDC dc (NULL );
HBITMAP hBmp = CreateDIBitmap (dc. m_hDC,
& BmiHeader,
CBM_INIT,
LpDIBBits,
& BmInfo,
DIB_RGB_COLORS );
File: // generate a bitmap handle
Bitmap. Attach (hBmp); // associate the handle with the defined Bitmap object
Array. RemoveAll (); file: // release memory

Return TRUE;

}
 

With the above preparations, we can now implement the image display function. Its implementation is as follows:

Void CImageDlg: OnShowImage ()
{
CimageData db; // defines the record set object
Db. Open (); Open the database
GetImageData (db. m_Images); // generates an image object based on the member variables of the record set object
File: // The image is displayed in the fixed area of the dialog box.
CPaintDC dc (this );
If (! (Bitmap. m_hObject = NULL ))
{CDC dcMem;
DcMem. CreateCompatibleDC (& dc); file: // create a Memory Image
CBitmap * pbmpOld;
Bitmap bmp size;
Bitmap. GetBitmap (& BMP Size); file: // get Image Size
PbmpOld = dcMem. SelectObject (& Bitmap );
Dc. StretchBlt (20, 20,200,200, & dcMem, 0, 0, BMP size. bmWidth, BMP size. bmHeight, SRCCOPY );
DcMem. SelectObject (pbmpOld );

}

The database used in the above Code is ACESS97. The program is compiled in Windows 98 and Visual C ++ 6.0, and runs normally.
 

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.