0
 public void RotateImage(Graphics paper,string theString)
        {
            try
            {
                using (paper)
                {

                    paper.RotateTransform(angle);
                    angle++;

                    sz = paper.MeasureString(theString, this.Font);


                    paper.DrawString(theString, new Font("Arial", 24), Brushes.Black, -(sz.Width / 2), -(sz.Height / 2));
                    paper.Dispose();
                }
            }
            catch (Exception e) { MessageBox.Show(e.ToString()); }

i get an error saying parameter is not valid, but only if i call my method for the second time in the paint method

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            RotateImage(e.Graphics, "A");
            RotateImage(e.Graphics, "B");

        }

if i call it once , the error will not happen.

 private void Form1_Paint(object sender, PaintEventArgs e)
            {
                RotateImage(e.Graphics, "A");

            }
2
Contributors
3
Replies
22
Views
3 Years
Discussion Span
Last Post by castajiz_2
Featured Replies
  • 1

    That's because you dispose of the graphics object in your RotateImage method. DOn't do that, the system needs it. Dispose of objects *you* create, not ones the system hands you. So get rid of the 'using' statement and the Dispose statement Read More

1

That's because you dispose of the graphics object in your RotateImage method. DOn't do that, the system needs it. Dispose of objects you create, not ones the system hands you.

So get rid of the 'using' statement and the Dispose statement

Edited by Momerath

0

i ve been struggling with the graphics class and yes removing the Dispose() does solve my problem but when the second method is called again somehow the other string that needs to be drawn loosses the rotation point , it does not rotate as it should , better said i does not rotate as the first string rotates.

0
public void RotateImage( string theString, int x, int y)  // napokon radi!
        {
            Graphics paper = CreateGraphics();
            paper.TranslateTransform(x, y);
            sz = paper.MeasureString(theString, stringFont);
            paper.RotateTransform(rtangle);
            rtangle += 10;
            paper.DrawString(theString, stringFont, new SolidBrush(Color.Blue), -(float)sz.Width / 2, -(float)sz.Height / 2);


        }

this solves my problem, removed the parameter e.graphics and always assign new graphics by creating it inside the method.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.