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");

            }

Recommended Answers

All 3 Replies

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.