I had this line of code:
bgpart = Primary.BackGround.Clone(rect, Primary.BackGround.PixelFormat);
That actually copies a part of the existing background. The purpose was to copy the part of the background my starship was drawn on, so the starship can be moved and, each time, the proper background to be drawn. Simply put, 'something' moves on a standard background, and the background remains.
Then this 'Out of memory' error of system.drawing start to manifest sometimes. When i focus on it, i realize that it has nothing to do with use up all memory. It should be called 'Out of memory RANGE', because what i found out is that on error cases, where i manage to emulate exactly, i asked him to read an image part that was actually outside the image itself, so the memory i ask him to read was not found into the memory of the object. Simply, the error raises when i moved the starship that was already out of the screen and into the screen, so ask him to copy a background non existent.
I replace my code with:
Bitmap bgpart = null;
try
{
bgpart = Primary.BackGround.Clone(rect, Primary.BackGround.PixelFormat);
}
catch(Exception e)
{
bgpart = Primary.BackGround.Clone(new Rectangle(0, 0, b.Width, b.Height), Primary.BackGround.PixelFormat);
}
So actually i sent him to read a surely existing part, and problem solved. In my case its ok because i try to read an non existent image part to draw it outside the screen - not needed anyways.
So these errors are (also) thrown from system.drawing when it is forced to read memory not supposed to.
If it is safe to bypass, that depends on usage of that readings.
In my case it was safe because i was trying to read and write non existent image parts.