如何在MFC中正确绘制位图并进行清理? [英] How do I properly draw a bitmap and cleanup after it, in MFC?

查看:99
本文介绍了如何在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中正确绘制位图并进行清理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆