当我使用位图作为鼠标光标时,为什么内存会迅速上升? [英] Why does the memory rise rapidly when I use a bitmap as a mouse cursor?

查看:123
本文介绍了当我使用位图作为鼠标光标时,为什么内存会迅速上升?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用位图作为鼠标光标时,内存正在快速上升。我不知道为什么,但我已经处理它。下面的代码:

代码正在运行以响应mousemove事件。



When I use a bitmap as a mouse cursor ,the memory is rising rapidly .I donot know why,but I have dispose it.The code below :
The codes is running to response to the mousemove event.

bmpCursor = global::XRF.Properties.Resources.Cross3;
Graphics g = Graphics.FromImage(bmpCursor);
Brush brush = new SolidBrush(Color.Black);

g.DrawString( (int)pf.X+"\r\n"+pf.Y, new Font(FontFamily.GenericSansSerif, 80f), brush, new PointF(0f, 0f));

//pictureBox1.Cursor.Dispose();
IntPtr hIcon = bmpCursor.GetHicon();
pictureBox1.Cursor = new Cursor(hIcon);//change the cursor of picturebox1 when moving mouse

brush.Dispose();
g.Dispose();

推荐答案

如果你正在查看任务管理器告诉你你的应用程序使用了多少内存,它就是骗人的您。它不知道.NET Framework是什么或它是如何工作的。它会向您显示.NET CLR为您的应用程序保留多少内存。



网上有关于此的各种文档,我不打算第1000次再次输入。



现在,在MouseMove事件中,为什么要不断为每个事件创建新的图形和画笔?别。当你需要创建它们时创建它们,然后当你不再需要它们时再处理它们。在每个MouseMove事件上执行此操作非常荒谬,因为您正在快速连续地创建和销毁对象,从而驱动应用程序正在使用的内存。
If you're looking at Task Manager to tell you how much memory your app is using, it's lying to you. It has no idea what the .NET Framework is or how it works. It's showing you how much memory is RESERVED by the .NET CLR for your app.

There's all kind of documentation on the web about this and I'm not going to type it all up again for the 1,000th time.

Now, in your MouseMove event, why are you constantly creating new Graphics and Brushes on every event? Don't. Create them ONCE, when you need to create them and then Dispose of them when you don't need them any more. Doing it on every MouseMove event is just ridiculous as you're creating and destroying the objects in rapid succession, thereby driving up the memory that your app APPEARS to be using.


不要这样做这个在Move事件中。在Mouse Enter事件上执行此操作(并在Mouse Leave事件中撤消它)。



如果特定小部件不提供鼠标进入/离开事件,请添加它们。



如果您使用鼠标移动事件,请按照Dave的建议进行操作,同时检查光标是否已经分配。如果光标已设置,则无需再次设置。
Don't do this on the Move event. Do so on the Mouse Enter event (and undo it on the Mouse Leave event).

If the particular widget doesn't provide mouse enter/leave events, add them.

If you're stuck using mouse move event, do as Dave suggests but also check to see if the cursor has already been assigned. If the cursor is already set, you don't need to set it again.


这篇关于当我使用位图作为鼠标光标时,为什么内存会迅速上升?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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