Archived

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

jperalta

Weird problem [C#]

Recommended Posts

The following code causes a fatal error in System.Drawing.DLL whenever I compile in Release, but not in Debug:
void LoadImages() {
	gunImage = Image.FromFile("images/gun.gif");
	for (int i = 0; i < 8; i++) {
		attackImages[i] = Image.FromFile("images/attack"+i+".gif",false);
		if (i < 7)
			ufoImages[i] = Image.FromFile("images/ufo" + i + ".gif",false);
		if (i < 4)
			explodeImages[i] = Image.FromFile("images/explode" + i + ".gif",false);
	}
}

Share this post


Link to post
Share on other sites
I know little of C# and it''s ways. However, that hasn''t stopped me from answering anything before, so... :\

It wouldn''t be something like the paths are relative, so they might end up pointing somewhere else in Release rather than Debug. e.g. "./Debug/images/attack0.gif" becomes "./Release/images/attack0.gif" during Release?

Ro_Akira

Share this post


Link to post
Share on other sites
That just breaks it more Basically, if the is thrown then the pictures aren't loaded and there are lots and lots of null reference exceptions

[edited by - jperalta on November 14, 2003 2:49:59 PM]

Share this post


Link to post
Share on other sites
Hrm... I guess the problem is actually as follows:

If I use the code above to load the images I get a System.OutofMemoryException at gunImage = Image.FromFile("images/gun.gif");

if I replace that with ''gunImage = new Bitmap("images/gun.gif");'' I get a System.ArgumentException (Invalid Parameter Used) in the same place instead of the out of memory exception.

Share this post


Link to post
Share on other sites
Again, I''d like to cover myself by declaring that I''ve never touched C#...

"If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception." - MSDN

Strange, but true. Who''d have thought an OutOfMemoryException could indicate that?!

Ro_Akira

Share this post


Link to post
Share on other sites
from the .NET framework documentation about the Image.FromFile() method:
quote:
.NET Framework Documentation
If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.


Yes docs are your friend. I''d find out which file formats and pixel formats GDI+ supports and check your file if it matches them.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When I used VJ++, my programs for some reason would run faster in debug than in release form. Odd stuff.

Maybe you should try deleting all the workspace/project files or cutting and pasting the source code into a new workspace/project.

Share this post


Link to post
Share on other sites
quote:

Why then, does it load the file in Debug but not in Release?



Indeed. This makes me think that it perhaps has something to do with the settings specified for a Release build?

It's not 100% clear from the MSDN docs, but I wonder does Image.FromImage throw OutOfMemoryException if the file doesn't exist at all?

The docs don't list any possible exceptions that Bitmap (String) might throw, but if your getting "Invalid Parameter Used", I'd guess that the actual path "images/gun.gif" is wrong. I'd double check yer files/paths.

quote:

what is gunImage? what is it defined as and where is it defined?


I'd guess it's of type System.Drawing.Bitmap. Bitmap (String) for example returns a System.Drawing.Image, but that's an abstract base class. Since there are exceptions emminating from both Bitmap(String), and Image.FromFile (), I think the problem lies there...

See disclaimers in above posts if you believe I'm talking rubbish.

Ro_Akira

[edited by - Ro_Akira on November 14, 2003 4:07:57 PM]

[edited by - Ro_Akira on November 14, 2003 4:08:41 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I experienced an out of memory exception when messing with images but only because I did not dispose of the bitmap that I was using for double-buffering. It was like this:

loop
create a bitmap
end loop

Which obviously created a new bitmap each frame. =X Your problem may not be in LoadImages, but somewhere else where there is looping.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
I experienced an out of memory exception when messing with images but only because I did not dispose of the bitmap that I was using for double-buffering. It was like this:

loop
create a bitmap
end loop

Which obviously created a new bitmap each frame. =X Your problem may not be in LoadImages, but somewhere else where there is looping.


Definately in LoadImages(), that function is only called once in the program (at the beginning) and it crashes as soon as it gets to gunImage = Image.FromFile...

[edited by - jperalta on November 14, 2003 4:33:09 PM]

Share this post


Link to post
Share on other sites
What it says about Image:
"An abstract base class that provides functionality for the Bitmap and Metafile descended classes" - MSDN

You should probably declare gunImage as System.Drawing.Bitmap? I don't see how it could produce the problems you're having but...

See disclaimers in above posts if you believe I'm talking rubbish.

[edit]
I think I may be talking a bit of rubbish. FromFile returns a derived class of Image, which might be Bitmap for example. But doing a gunImage.GetType().ToString(); and dumping it out will reveal it to be not really an Image, but of type 'whatever FromFile returned'. Regardless, this is all irrelevant to the problem...
[/edit]

Ro_Akira

[edited by - Ro_Akira on November 14, 2003 5:00:16 PM]

Share this post


Link to post
Share on other sites

void Init()
{
// Basic form setup.
this.BackColor = Color.Black;
this.Show();

// Load UFO images
LoadImages();
// Event handlers
#if !(DIRECTINPUT)
this.MouseMove += new MouseEventHandler(mouseMove);
this.MouseDown += new MouseEventHandler(MousePress);
#else
InitializeDirectInput();
#endif
this.KeyDown += new KeyEventHandler(KeySet);
this.KeyUp += new KeyEventHandler(KeyRelease);
this.Closing += new CancelEventHandler(Stop);

// Setup DirectDraw
InitializeDirectDraw();
s = new Size(this.Width,this.Height);

// Initialize the game managers
um = new UFOManager(s,ufoImages,attackImages,explodeImages);
gm = new GunManager(s,gunImage,um.UFOS);
um.Initialize(gm);

// Start the game
start = new ThreadStart(Run);
animation = new Thread(start);
animation.Start();
}

Share this post


Link to post
Share on other sites
I''m stumped. Last thing I could think of is try this when you load the image, both methods (the Image.FromFile , and the Bitmap Constructor):
gunImage = Image.FromFile(Application.StartupPath + "images/gunImage.gif");

and make sure the image is in the images folder in the app directory.

Share this post


Link to post
Share on other sites