I am creating a dialog based application in mfc.
I have two button and one picture control.
I want to save and retrieve bmp into(from) ms access 2007.
In database, I have field(picture,OLE Object)
In save button click
CDaoDatabase* m_database= new CDaoDatabase();
m_database->Open(L"C:\\Users\\ABC\\ABC.mdb",TRUE,FALSE,L"");
if(m_database->IsOpen())
{
CString strSQL =_T("SELECT picture FROM Table2");
CDaoRecordset m_reordset(m_database);
m_reordset.Open(AFX_DAO_USE_DEFAULT_TYPE,strSQL,0);
int iRecords =m_reordset.GetRecordCount();
if (!iRecords)
{
m_reordset.AddNew();
CPictureHolder pic;
CBitmap bmp;
bmp. LoadBitmap (IDB_BITMAP1);
if (pic.CreateFromBitmap (&bmp))
{
IStream* pStream;
CreateStreamOnHGlobal (NULL, TRUE, &pStream);
if (pStream)
{
LONG sz;
pic.m_pPict-> SaveAsFile (pStream, TRUE, &sz);
HGLOBAL hGlobal;
GetHGlobalFromStream (pStream, &hGlobal);
CByteArray barray;
CLongBinary bin;
bin.m_dwDataLength = GlobalSize (hGlobal);
bin.m_hData = hGlobal;
m_reordset.SetFieldValue (_T("picture"), COleVariant (bin)); }
m_reordset.Update();
}
}
m_database->Close();
picture field is showing "Long binary data".
In retrieve button click
CDaoDatabase* m_database= new CDaoDatabase();
COleVariant valuer;
m_database->Open(L"C:\\Users\\ABC\\ABC.mdb",TRUE,FALSE,L"");
if(m_database->IsOpen())
{
CString strSQL =_T("SELECT picture FROM Table2");
CBitmap ABitmap;
BITMAP bm;
CDaoRecordset m_reordset(m_database);
m_reordset.Open(AFX_DAO_USE_DEFAULT_TYPE,strSQL,0);
int iRecords =m_reordset.GetRecordCount();
valuer=m_reordset.GetFieldValue(0);
u_long pcBufLen =valuer.parray->rgsabound[0].cElements;
BYTE *ppBuf = new BYTE[pcBufLen]; BYTE *tmp= NULL;
if( valuer.vt!=VT_NULL)
{
void* pArrayData;
CByteArray Photo;
const void* lpbits;
Photo.SetSize(pcBufLen);
SafeArrayAccessData(valuer.parray,&pArrayData);
memcpy(Photo.GetData(),pArrayData,Photo.GetSize());
memcpy(&bm,pArrayData,sizeof(BITMAP));
HDC hdc,hdcMem;
HWND hh=::GetDlgItem(GetSafeHwnd(),IDC_pic);
hdc = ::GetWindowDC(hh);
HBITMAP hBitmap=NULL;
hdcMem = ::CreateCompatibleDC(hdc);
SelectObject(hdcMem,::CreateBitmap(bm.bmWidth,bm.bmHeight,bm.bmPlanes,bm.bmBitsPixel,bm.bmBits));
int res=BitBlt(hdc, 0,0, bm.bmWidth, bm.bmHeight,hdcMem,0,0,SRCCOPY);
hBitmap=CreateCompatibleBitmap(hdc,100,100);
HBITMAP t=m_nHeight.SetBitmap(hBitmap);
DeleteDC(hdcMem );
SafeArrayUnaccessData(valuer.parray);
}
m_database->Close();
m_nHeight is CStatic class for IDC_pic control
value of t is 0x00000000 {unused=??? }
and
bm.bmType=137776450
bm.bmWidth=7
bm.bmHeight=3538944
bm.bmWidthBytes=2621440
bm.bmPlanes=0
bm.bmBitsPixel=320
bm.bmBits=0x01e00000
I am not getting image on picture control, its getting black screen.
I think problem is in bm.bmPlanes.
Help me!