Handling bitmap data in Access with VC
Epilepyman published in 2000-08-17 05:49:58:
---- More complex databases generally have bitmap data (such as photos). Although the insertion, deletion, and replacement of this type of "OLE object" is easy to implement in Access, it is more complicated in VC and is quite average. Hereinafter, the author uses the VC to process the bitmap data in the Access database, so as to ask for everyone.
---- In the object of CDAORECORDSET derived class, VC automatically generates a Clongbinary object for Access's "Ole Object" domain. Although this class is simpler, it is necessary to use global function GlobalAlloc () and globalfree () processing with its memory handle M_HData, and send Globalock () and GlobalUnlock (), and give it m_dwdatales assigns the value, which is relatively complicated, so it is generally recommended to use the CBYTEARRAY class. This only needs to modify it in the data instructions of the CDaorecordset derived class, and change DFX_LONGBINARY () in DofieldExchange () to DFX_BINARY ().
---- Author defines a CDIB class with COBJECT (CDAORECORDVIEW defined in the derived class of CDIB objects), including membership and methods:
CBYTEARRAY M_BUFDIB;
Bool Create (CBYTEARRAY & BA);
Bool Create (CFILE & BMPFILE);
BOOL Paint (HDC HDC);
---- m_bufdib is a buffer that stores bitmap data. For the sake of simplicity, it does not contain packaging information and BitmapFileHeader structure. After updating the database, the original "OLE object" type will become "long binary data" and cannot be viewed in Access.
---- The parameter BA of the first create () overload method is a bitmap data (such as m_image) of the record, copies the data to m_bufdib using CByteArray :: Copy (); the parameter of the second Create () method BMPFile is the open bitmap file, read the data in the file in M_BUFDIB (abandon the previous BitmapFileHeader structure):
DWORDDWBUFSIZE;
dwbufsize = bmpfile.getlength (); // Get file length
Bmpfile.seek ((long) Sizeof (BitmapfileHeader),
Cfile :: begin); // Abandon the file
dwbufsize- = sizeof (bitmapfileheader);
m_bufdib.setsize (dwbufsize); // Set buffer size
File.readhuge ((LPSTR) (m_bufdib.getdata ()), dwbufsize;
......
---- Paint () method calls the setDibitStodevice () function (depending on the situation, streamDibits ()), the parameter HDC is a static control device handle in the resource of CDAORECORDVIEW, as the first parameter of SetDibitStodevice (). If not 16 or 24 bitmaps, you also need to create and set up a palette. The Paint () method is called in addition to the onmove () of the CDAORECORDVIEW derived class, it is also called (preferably not in ondraw ():
Void CDeriveDView :: onpaint ()
{
CPAINTDC DC (this);
CClientDC DC1 (& M_CTLIMAGE); if (m_dib.create (m_pset-> m_image))
m_dib.paint (dc1.m_hdc);
}
---- The method first adopted is that whenever a bitmap file is opened, m_dib.create () and m_dib.paint (), then copy it to m_pset-> m_image, then set "Dirty" ID:
IF (m_dib.create (bmpfile))
{
CClientDC DC (& M_CTLIMAGE);
m_dib.paint (dc.m_hdc);
(m_pset-> m_image) .copy (m_dib.m_bufdib);
SetFieldDiRTY (& (M_Pset-> m_image));
}
---- When the record is scrolled, OnMove () calls Update () updates the data.
---- But the result of this is that the data can only be updated only when the content is not empty (null). That is to say, adding "long binary data" cannot be implemented.
---- Finally found seitevalue () can be added and replaced. But because the author is unknown, it is necessary to set another domain as "dirty":
IF (m_dib.create (bmpfile))
{
CClientDC DC (& M_CTLIMAGE);
m_dib.paint (dc.m_hdc);
(m_pset-> m_image) .copy (m_dib.m_bufdib);
// Use only for onpaint () call
m_pset-> setfieldValue (_t ("[Image]"),
Colevariant (m_dib.m_bufdib);
m_pset-> setfielddirty (& (m_pset-> m_name));
// All other domains
}
---- If you plan to delete bitmap data in the database, you can replace a "empty" CBYTEARRAY object.
Beijing University of Applied Arts Arts
Su Hongrui
*****