Deserialize Bitmap from Archive
I've got a problem with my MFC app. When I'm trying to deserialize CBitmap from the archive and create new CBitmap, it doesn't properly load CBitmap's bits.
Here's the code:
BITMAP bm;
ar >> bm.bmType;
ar >> bm.bmWidth;
ar >> bm.bmHeight;
ar >> bm.bmWidthBytes;
ar >> bm.bmPlanes;
ar >> bm.bmBitsPixel;
int nSize = bm.bmWidth * bm.bmHeight;
bm.bmBits = new BYTE[nSize];
ar.Read(bm.bmBits, nSize);
CBitmap* tmp = new CBitmap;
tmp->CreateBitmapIndirect(&b开发者_如何学Pythonm);
BITMAP bmi;
tmp->GetBitmap(&bmi);
HBITMAP hNew = (HBITMAP)CopyImage((HBITMAP)(*tmp), IMAGE_BITMAP,
bmi.bmWidth, bmi.bmHeight, LR_CREATEDIBSECTION);
m_bmp.Attach(hNew);
delete tmp;
After I do tmp->GetBitmap(&bmi); I get NULL into bmi.bmBits field.
What's wrong with that? How can I make it work?
P.S. I mustn't use serialization into *.bmp file.
Thanks in advance, Mike.
If you can change your serialization this will work, bmp
is HBITMAP
:
// store
CImage image;
image.Attach(bmp);
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, 0);
IStream* pStream;
CreateStreamOnHGlobal(hMem, TRUE, &pStream);
image.Save(pStream, Gdiplus::ImageFormatBMP);
size_t nSize = GlobalSize(hMem);
LPVOID buff = GlobalLock(hMem);
ar << nSize;
ar.Write(buff, nSize);
GlobalUnlock(hMem);
GlobalFree(hMem);
// load
size_t nSize;
ar >> nSize;
HGLOBAL hMem = GlobalAlloc(GHND, nSize);
LPVOID buff = GlobalLock(hMem);
ar.Read(buff, nSize);
GlobalUnlock(hMem);
IStream* pStream;
CreateStreamOnHGlobal(hMem, TRUE, &pStream);
CImage image;
image.Load(pStream);
bmp = image.Detach();
GlobalFree(hMem);
精彩评论