2012-04-03 460 views
0

我试图在面板上绘图时似乎出现此错误。我没有C#的专家,所以希望这里有人能帮助我。提前致谢。System.InvalidOperationException:对象当前正在其他地方使用

堆栈跟踪显示,

用C

在System.Drawing.Graphics.set_Transform在Victoria.Robotics.Marvin.Teleoperation.MainForm.DrawXYAxis(图形克)(矩阵值):\用户\ kasunt \微软Robotics Dev Studio 2008 R3 \ Marvin \ Teleoperation \ MainForm.cs:line 2173 at Victoria.Robotics.Marvin.Teleoperation.MainForm.envMap_Paint(Object sender,PaintEventArgs e)in C:\ Users \ kasunt \ Microsoft Robotics Dev Studio 2008 R3 \在System.Windows.Forms的System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16图层,布尔disposeEventArgs)System.Windows.Forms.Control.OnPaint(PaintEventArgs e)上的第2143行:Marvin \ Teleoperation \ MainForm.cs: .Control.WmPaint(Message & m)at System.Windows.Forms.Control.WndProc(Message &米)在System.Windows.Forms.ScrollableControl.WndProc(消息& m)上System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m)上System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m)在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

代码如下。错误似乎设置变换时出现,

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(); 
    } 

线#2173,

 g.Transform = mx; 

线#2143,

 DrawXYAxis(e.Graphics); 
+0

发表您的线#2173从MainForm.cs – 2012-04-03 13:37:55

+0

和线#2143。 – 2012-04-03 13:39:14

+0

完成:)。谢谢 – nixgadgets 2012-04-03 13:42:25

回答

3

这是一个选项通常通过非法使用图形出现异常上下文在另一个线程。我不得不猜测你的程序中还有其他代码也在使用图形。

如果您不知道代码可能是什么,请使用Debug + Windows + Threads并查看列出的线程的调用堆栈。也可以在源代码中查找Control.CheckForIllegalCrossThreadCalls的任何分配。您需要删除该语句,以便在违反线程要求时获得更好的诊断。

+0

这使我走上了正确的道路;) – nixgadgets 2012-04-03 22:40:27

0

原来是因为以下两行,

IntPtr hdc = g.GetHdc(); 
    int hMemDC = hdc.ToInt32(); 
相关问题