Out of Memory Exception in a user drawn control

G

Guest

Hello,

I am drawing a control with GDI+. It contains an image which turns around a
center.

After turning for a while I get and "Out of Memory" Exception. I am
disposing all the created objects(the ones which are used/created during
turning).

The image is included in the solution as an embedded resource, loaded into a
stream and than created with new Bitmap. But it is always the same image and
I am laoding it only once.

When the Exception occurs the Control is just lost on the screen and instead
i see red framed square with is crossed out.

The Code where the Exception is thrown:

private void OnPaint(Graphics graphics)
{
Matrix matrix = new Matrix();
matrix.RotateAt(Angle, m_center, MatrixOrder.Append);
graphics.Transform = matrix;
graphics.DrawImage(m_image, m_rect);
matrix.RotateAt(-Angle, m_center, MatrixOrder.Append);
graphics.Transform = matrix;
matrix.Dispose();
}

Thanks for any help
Alex
 
G

Guest

Alex said:
Hello,

I am drawing a control with GDI+. It contains an image which turns around a
center.

After turning for a while I get and "Out of Memory" Exception. I am
disposing all the created objects(the ones which are used/created during
turning).

The image is included in the solution as an embedded resource, loaded into a
stream and than created with new Bitmap. But it is always the same image and
I am laoding it only once.

When the Exception occurs the Control is just lost on the screen and instead
i see red framed square with is crossed out.

The Code where the Exception is thrown:

private void OnPaint(Graphics graphics)
{
Matrix matrix = new Matrix();
matrix.RotateAt(Angle, m_center, MatrixOrder.Append);
graphics.Transform = matrix;
graphics.DrawImage(m_image, m_rect);
matrix.RotateAt(-Angle, m_center, MatrixOrder.Append);
graphics.Transform = matrix;
matrix.Dispose();
}

Thanks for any help
Alex

You are disposing the Matrix object taht you just assigned to the
Graphics object. That means that the Graphics object contains a Matrix
object that is no longer valid.

Get the Matrix object that the Graphics object contains from start, then
put that back when you are done.
 
G

Guest

Göran Andersson said:
You are disposing the Matrix object taht you just assigned to the
Graphics object. That means that the Graphics object contains a Matrix
object that is no longer valid.

Get the Matrix object that the Graphics object contains from start, then
put that back when you are done.

I did that but It didn't solve the problem, I still get the Out of Memory
Exception after turning the object several times.

{
Matrix matrix = new Matrix();
Matrix save = graphics.Transform;
matrix.RotateAt(Angle, m_center, MatrixOrder.Append);
graphics.Transform = matrix;
graphics.DrawImage(m_image, m_rect);
matrix.RotateAt(-Angle, m_center, MatrixOrder.Append);
graphics.Transform = matrix;
graphics.Transform = safe;
matrix.Dispose();
}
 
G

Guest

After further investigation of the Problem I thought the title is misleading,
I opened a new thread "Graphics.DrawImage throws "Out of Memory" Exception".
 
G

Guest

Alex said:
I did that but It didn't solve the problem, I still get the Out of Memory
Exception after turning the object several times.

{
Matrix matrix = new Matrix();
Matrix save = graphics.Transform;
matrix.RotateAt(Angle, m_center, MatrixOrder.Append);
graphics.Transform = matrix;
graphics.DrawImage(m_image, m_rect);
matrix.RotateAt(-Angle, m_center, MatrixOrder.Append);
graphics.Transform = matrix;
graphics.Transform = safe;
matrix.Dispose();
}

You put the matrix in the variable "save", then try to read it from the
variable "safe"?

I suppose that's just a typo, but you should consider to post the code
that you are actually using instead of some other code that you are not
using. Noone can find an error in your code by looking at some other code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top