Archived

This topic is now archived and is closed to further replies.

wakebrdkid12

Memory with Sprite class in C#

Recommended Posts

wakebrdkid12    122
Hey, I''ve been going through the book .NET Game Programming in C# and have successfully finished my first Tetris clone in chapter 1. In chapter 2 I will be making a Snake type game. I am having a problem though. Whenever I run my program I get a System out of Memory exception and it points to this area of my code in the Sprite class:
public void Draw(System.IntPtr WinHandle) 
{
	using (Graphics graphBack = Graphics.FromHwnd(WinHandle))
	{
		graphBack.DrawImage(Source, new Rectangle(Location.X*(int)Scale,
		Location.Y*(int)Scale, IMAGE_SIZE, IMAGE_SIZE),
		new Rectangle(Location.X*(int)Scale,
		Location.Y*(int)Scale, IMAGE_SIZE, IMAGE_SIZE),
		GraphicsUnit.Pixel);
	}
}
I don''t know why I would get a system out of memory exception because the using expression is supposed to automatically dispose of the graphics object when its done. Any ideas? I can post some more of my code if necessary. Thanks.

Share this post


Link to post
Share on other sites
Matei    190
Which line in this area? Also, are you sure this is the right way of painting in C#? I would do something like override the paint event handler, which means you don''t have to get the graphics context and dispose every time.

Share this post


Link to post
Share on other sites
wakebrdkid12    122
The specific line it refers to is the:

using (Graphics graphBack = Graphics.FromHwnd(WinHandle))

This function is for drawing a specific graphic to part of the window. Its part of the Sprite class.

Share this post


Link to post
Share on other sites