Jump to content
  • Advertisement

Archived

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

wakebrdkid12

Memory with Sprite class in C#

This topic is 5137 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!