从存档中反序列化位图

发布于 2024-08-14 07:40:03 字数 784 浏览 2 评论 0原文

我的 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。

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

PS 我不能将序列化为 *.bmp 文件。

预先感谢,迈克。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-08-21 07:40:03

如果您可以更改序列化,这将起作用,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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文