如何在没有内存泄漏且无响应的情况下使用内存DC绘制连续位图 [英] how to draw continuous bitmap by using memory dc whithout memory leak and no response

查看:76
本文介绍了如何在没有内存泄漏且无响应的情况下使用内存DC绘制连续位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,
尝试绘制连续位图时,我遇到内存泄漏且没有响应.
(首先,我想承认我不知道复杂的命令,而我只能使用几个命令.因此,如果我的问题太简单,请不要介意.)
这是我尝试的过程.

我想在固定的位图上绘制随机的颜色透明像素.我在基于MFC对话框的应用程序的OnPaint事件中编写了以下代码.然后,我可以看到想要的内容(固定位图上的随机彩色透明像素).

Hi Friends,
I face memory leak and no response when I try to paint continuous bitmap.
(Firstly, I would like to admit that I don''t know complicated commands and I just able to use few commands. So, please don''t be mind if my question is too simple.)
Here is the process that I try.

I would like to draw random color transparent pixel on fixed bitmap. I wrote the following code in MFC Dialog-based Application, OnPaint event. Then, I can see what I want(random color transparent pixel on fixed bitmap).

CDC dc;
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP_TESTBITMAP);
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
CDC* pDC = this->GetDC();
dc.CreateCompatibleDC(pDC);
CBitmap* pOldBmp = dc.SelectObject(&bmp);
pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dc, 0, 0, SRCCOPY);

for (int i = 0; i < bmpInfo.bmHeight; i++) {
    for(int j = 0; j < bmpInfo.bmWidth; j++) {
	dc.SetPixel(j, i, RGB(rand()%100, rand()%50, rand()%100)); // ●●it is temporary test code (Actually, I will read file data in here)
    }
}
pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dc, 0, 0, SRCAND);
		
dc.SelectObject(pOldBmp);
bmp.DeleteObject();
dc.DeleteDC();
this->ReleaseDC(pDC);

CDialogEx::OnPaint();



在下一步中,无论何时将新文件数据添加到文件夹中,我都希望从文件数据中设置像素.所以,我用



In next step, I would like to set pixel from file data whenever the new file data is added to folder. So, I use

do {
    ReadDirectoryChanges(...); // folder path and necessary parameters are passed
    pInfo = (FILE_NOTIFY_INFORMATION*) &buffer;
    If (pInfo->Action == FILE_ACTION_ADDED) {
         // ●●I will read file data and set pixel of file data here, the background IDB_BITMAP_TESTBITMAP is fixed as before.
    }


但是问题是按上述代码更改程序并运行后,仅显示第一个文件数据,此后没有响应.并且还会发生内存.我的代码有什么大错误?
非常感谢您的时间和帮助.


But the problem is after changing the program as above code and run it, just only the first file data is shown and no response after that. And also memory is occurred. What''s big mistake with my code?
Thanks in advanced for your time and help.

推荐答案

您需要通过调用CWNd :: Invalidate()或更好的 ^ ]
You need to trigger the drawing again, by calling CWNd::Invalidate() or better http://msdn.microsoft.com/en-US/library/2f3csed3(v=vs.80).aspx[^]


这篇关于如何在没有内存泄漏且无响应的情况下使用内存DC绘制连续位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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