2012-04-03 10 views
0

Ich erhalte diesen Fehler, wenn ich versuche, auf einem Panel zu zeichnen. Ich bin kein Experte in C#, also hoffentlich kann mir hier jemand helfen. Danke im Voraus.System.InvalidOperationException: Objekt wird derzeit an anderer Stelle verwendet

Die Stack-Trace zeigt,

bei System.Drawing.Graphics.set_Transform (Matrix-Wert) bei Victoria.Robotics.Marvin.Teleoperation.MainForm.DrawXYAxis (Graphics g) in C: \ Users \ kasunt \ Microsoft Robotics Dev Studio 2008 R3 \ Marvin \ Teleoperation \ MainForm.cs: Zeile 2173 bei Victoria.Robotics.Marvin.Teleoperation.MainForm.envMap_Paint (Objektabsender, PaintEventArgs e) in C: \ Benutzer \ kasunt \ Microsoft Robotics Dev Studio 2008 R3 \ Marvin \ Teleoperation \ MainForm.cs: Zeile 2143 bei System.Windows.Forms.Control.OnPaint (PaintEventArgs e) unter System.Windows.Forms.Control.PaintWithErrorHandling (PaintEventArgs e, Int16-Ebene, Boolean deposeEventArgs) bei System.Windows.Forms .Control.WmPaint (Nachricht & m) bei System.Windows.Forms.Control.WndProc (Nachricht & m) bei System.Windows.Forms.ScrollableControl.WndProc (Message & m) bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m) bei System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m) bei System.Windows.Forms.NativeWindow.Callback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Der Code ist unten. Fehler scheint aufzutreten, wenn die Transformation Einstellung

private void envMap_Paint(object sender, PaintEventArgs e) 
    { 
     DrawXYAxis(e.Graphics); 
    } 

    /// <summary> 
    /// Helper to draw the XY axis and plot the map 
    /// </summary> 
    public void DrawXYAxis(Graphics g) 
    { 
     Rectangle rect = envMap.ClientRectangle; 

     myPen = new Pen(Color.Black, 1); 
     g.PageUnit = GraphicsUnit.Millimeter; 
     g.PageScale = 0.1F; 
     IntPtr hdc = g.GetHdc(); 
     int hMemDC = hdc.ToInt32(); 

     // Reverse the axis of the drawing surface 
     Matrix mx = new Matrix(1, 0, 0, -1, 0, envMap.ClientSize.Height * 2); 
     g.Transform = mx; 
     g.TranslateTransform(50, 100, MatrixOrder.Append); 

     // For drawing X - AXIS 
     g.DrawLine(myPen, 0, 0, (2 * rect.Right - 60), 0); 
     // For drawing Y - AXIS 
     g.DrawLine(myPen, 0, 0, 0, 2 * rect.Bottom); 

     // For drawing Arrow on X-AXIS 
     g.DrawLine(myPen, (2 * rect.Right - 60) - 15, 8, (2 * rect.Right - 60), 0); 
     g.DrawLine(myPen, (2 * rect.Right - 60), 0, (2 * rect.Right - 60) - 15, -8); 

     // For drawing Arrow on Y-AXIS 
     g.DrawLine(myPen, 8, 2 * rect.Bottom - 15, 0, 2 * rect.Bottom); 
     g.DrawLine(myPen, 0, 2 * rect.Bottom, -8, 2 * rect.Bottom - 15); 

     // Save the state to restore later 
     GraphicsState state = g.Save(); 

     // Create a matrix to offset the text to the desired position and flip it the 
     // right way up again 
     Matrix mx2 = new Matrix(1, 0, 0, -1, 0, 0); 
     Matrix mx1 = mx.Clone(); 
     mx1.Multiply(mx2); 
     g.Transform = mx1; 
     SolidBrush drawBrush = new SolidBrush(Color.Black); 
     Font drawFont = new Font("Microsoft Sans Serif", 9, FontStyle.Bold); 
     StringFormat sF = new StringFormat(StringFormatFlags.NoClip); 
     sF.Alignment = StringAlignment.Center; 
     g.DrawString("X", drawFont, drawBrush, (2 * rect.Right - 40), -2 * Font.Height, sF); 
     g.DrawString("Y", drawFont, drawBrush, -40, -(2 * rect.Height + 2 * Font.Height), sF); 

     // Restore state 
     g.Restore(state); 

     drawFont = new Font("Microsoft Sans Serif", 7); 

     // Drawing Tick Marks and Labels 
     // NOTE THE LABELS ON THE AXES WILL CHANGE TO REFFECT THE REAL POSITION OF THE ROBOT.... 
     myPen.Dispose(); 
    } 

Linie # 2173,

 g.Transform = mx; 

Linie # 2143,

 DrawXYAxis(e.Graphics); 
+0

posten Sie Ihre Linie # 2173 von MainForm.cs –

+0

und # 2143 Linie. –

+0

erledigt :). Danke – nixgadgets

Antwort

3

Dies ist eine Ausnahme, die von der Regel verursacht wird illegal eine Grafik mit Kontext in einem anderen Thread. Ich müsste raten, dass es in Ihrem Programm einen anderen Code gibt, der auch etwas mit Grafiken macht.

Wenn Sie keine Ahnung haben, was dieser Code sein könnte, dann verwenden Sie Debug + Windows + Threads und sehen Sie sich die Aufruflisten der dort aufgelisteten Threads an. Suchen Sie auch nach einer Zuordnung zu Control.CheckForIllegalCrossThreadCalls in Ihrem Quellcode. Sie sollten diese Anweisung löschen, damit Sie bessere Diagnosen erhalten, wenn Sie die Threading-Anforderungen verletzen.

+0

Das bringt mich auf den richtigen Weg;) – nixgadgets

0

Es stellte sich aufgrund der folgenden zwei Zeilen heraus,

IntPtr hdc = g.GetHdc(); 
    int hMemDC = hdc.ToInt32();