如何在MFC中正确绘制位图并进行清理?

编程入门 行业动态 更新时间:2024-10-25 06:30:43
本文介绍了如何在MFC中正确绘制位图并进行清理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的第一次提交,所以如果我做错了,我会在前面道歉。 我在Windows应用程序中使用MSVC C ++有MFC支持。我花了最近3天在线寻找建议,下面的代码是我提出的。 imgOut是在调用此例程之前创建的全局CImage对象。一般来说代码工作正常,但是在不到30000个周期后我收到一个错误: Centroider.exe中0x516b70ee(mfc90d.dll)的未处理异常:0xC0000005:访问冲突读取位置0x0000000000000008。 调用堆栈指向的位置:BOOL delDC = DeleteDC(mDC); 从评论的行中可以看出,我已经尝试了各种方法进行清理,没有任何作用。 我会非常感谢建议。 /> 谢谢

This is my first submission, so I apologize up front if I'm doing it wrong. I'm working in MSVC C++, in a Windows application with MFC support. I have spent the last 3 days online looking for suggestions, and the code below is what I have come up with. imgOut is a global CImage object created before this routine is called. In general the code works fine, but at just under 30000 cycles I get an error: Unhandled exception at 0x516b70ee (mfc90d.dll) in Centroider.exe: 0xC0000005: Access violation reading location 0x0000000000000008. Which the call stack points to: BOOL delDC = DeleteDC(mDC); As you can see from the commented lines, I have tried a variety of approaches at cleaning up and nothing works. I would greatly appreciate suggestions. Thanks

void DrawBitmap::DrawImg(){ try{ drawCnt++; CDC *screenDC = dlgPWnd->GetDC(); CDC mDC; mDC.CreateCompatibleDC(screenDC); CBitmap b; b.CreateCompatibleBitmap(screenDC, bmpXDest, bmpYDest); CBitmap *pob = mDC.SelectObject(&b); mDC.SetStretchBltMode(HALFTONE); imgOut.StretchBlt(mDC.m_hDC, 0, 0, bmpXDest, bmpYDest, 0, 0, imgOut.GetWidth(), imgOut.GetHeight(), SRCCOPY); mDC.SelectObject(pob); HWND pictHW = GetDlgItem( dlgHWnd, pictIdc ); CWnd* pictCW = CWnd::FromHandle(pictHW); BOOL delObj = DeleteObject(((CStatic*)pictCW)->SetBitmap((HBITMAP)b.Detach())); pob->Detach(); //mDC.DeleteDC(); BOOL delDC = DeleteDC(mDC); //mDC.Detach(); //mDC.ReleaseAttribDC(); //mDC.ReleaseOutputDC(); //DeleteObject(mDC); imgOut.Detach(); dlgPWnd->ReleaseDC(screenDC); }catch(...){ TRACE("DrawImg Exception\n"); } }

推荐答案

那里有一些非常复杂的代码,但它可能在以下内容中: Some really convoluted code there but it could be in the following: // statements like this are almost impossible to debug or read. BOOL delObj = DeleteObject(((CStatic*)pictCW)->SetBitmap((HBITMAP)b.Detach())); pob->Detach();

你在对象上调用 Detach b ,然后立即在 pob 上调用它,指向 b ,不再有效。

You call Detach on the object b, and then immediately call it on pob which points to b, which is no longer valid.

更多推荐

如何在MFC中正确绘制位图并进行清理?

本文发布于:2023-10-16 11:52:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497473.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   正确   如何在   MFC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!