Storing "Paint Brush" images in a Access DB

This example demonstrates how to store a "Paint Brush" image in a Access DB. In my code I have created a CDaoRecordset derived class for handling and reading the binary data. Class Wizard binds a CLongBinary to the Ole Object field when creating the recordset. CLongBinary is difficult to manage, so in the recordset, that is to receive the binary data, I bind a CByteArray variable to the field.

Example :

::DoFieldExchange(CDaoFieldExchange* pFX) 

DFX_LongBinary(pFX, _T("[Images]"), m_Images);

become

DFX_Binary(pFX, _T("[Images]"), m_Images); 

In CDAORecordSet the public member "CLongBinary m_Images" become "CByteArray m_Images"

The function code is :
BOOL CBLOBSDlg::ReadFromBLOB(CByteArray & DBArray) 
{ 
  CByteArray Array; 
  Array.Copy( DBArray); 
  // the header of BLOB is OLE stuff like "Paint Brush Application" .... ecc.. 
  // the len is 78 byte ( I do not know for other headers ) 
  int HeaderLen =   78 + sizeof(BITMAPFILEHEADER); 
  Array.RemoveAt( 0, HeaderLen ); // I cut all Headers 
  
  // some BMP information 
  BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)Array.GetData() ; 
  BITMAPINFO &bmInfo = *(LPBITMAPINFO)Array.GetData() ; 
  
  
  // If bmiHeader.biClrUsed is zero we have to infer the number 
  // of colors from the number of bits used to specify it. 
  int nColors = bmiHeader.biClrUsed ? bmiHeader.biClrUsed : 1 << bmiHeader.biBitCount; 

  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); 
  

  CClientDC dc(NULL); 
  
  HBITMAP hBmp = CreateDIBitmap( dc.m_hDC,   // handle to device context 
                                 &bmiHeader,     // pointer to bitmap size and format data 
                                 CBM_INIT,       // initialization flag 
                                 lpDIBBits,      // pointer to initialization data 
                                 &bmInfo,        // pointer to bitmap color-format data 
                                 DIB_RGB_COLORS);                // color-data usage 
  
  OleBmp.Attach( hBmp ); 

  Array.RemoveAll(); //Relese Memory 

  return TRUE; 
} 

Note:

1: In the example the DB path in "CString CDaoRecorset::GetDefaultDBName()" must be changed!!

2: This code is for VC++ 5

Download source - 36KB

Update regarding storing a BLOB in an Access DB:
Download Project including saving BLOB - 60 KB

Date updated : 12 Feb. 1999



Comments

  • tee shirt diesel homme tee shirt perso

    Posted by efmzboyt on 03/19/2013 08:10am

    ÿþ

    Reply
  • tee shirt handball tee shirt dinosaure

    Posted by nnzzqzzl on 03/18/2013 05:56am

    ÿþ

    Reply
  • impression de tee shirt pas cher tee shirt creation

    Posted by miqjijyz on 03/17/2013 10:39pm

    ÿþ

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Live Event Date: August 19, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You deployed your app with the Bluemix PaaS and it's gaining some serious traction, so it's time to make some tweaks. Did you design your application in a way that it can scale in the cloud? Were you even thinking about the cloud when you built the app? If not, chances are your app is going to break. Check out this upcoming eSeminar to learn various techniques for designing applications that will scale successfully in Bluemix, for the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds