从存档反序列化位图

时间:2009-12-13 20:30:25

标签: mfc gdi

我的MFC应用程序出了问题。当我试图从归档中反序列化CBitmap并创建新的CBitmap时,它无法正确加载CBitmap的位。

以下是代码:

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(&bm);
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;

我做tmp-> GetBitmap(& bmi);我在bmi.bmBits字段中得到NULL。

这有什么问题?我怎样才能使它发挥作用?

P.S。我不能将序列化用于* .bmp文件。

先谢谢你,迈克。

1 个答案:

答案 0 :(得分:0)

如果您可以更改序列化,这将有效,bmpHBITMAP

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