Click here to Skip to main content
15,895,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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();
Posted

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.
 
Share this answer
 
Comments
codeprojectddx 9-Sep-14 2:33am    
My goals is that I want to capture the point where the mouse is,whenever the mouse moves,the point changes and I want show the coordinates on the cursor.I know that,the cursor is really a bitmap,as a result I operate the bitmap and create and destroy the bitmap when the mouse moves. But I donot have a better idea.
Dave Kreskowiak 9-Sep-14 8:01am    
The code isn't really complete, but are you Disposing the Bitmap you created?? It doesn't look like it!
CPallini 12-Sep-14 13:37pm    
5.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900