【问题标题】:System.InvalidOperationException: Object is currently in use elsewhereSystem.InvalidOperationException:对象当前正在其他地方使用
【发布时间】:2012-04-03 13:33:09
【问题描述】:

尝试在面板上绘图时,我似乎遇到了这个错误。我不是 C# 方面的专家,所以希望这里有人可以帮助我。提前致谢。

堆栈跟踪显示,

在 System.Drawing.Graphics.set_Transform(Matrix value) at Victoria.Robotics.Marvin.Teleoperation.MainForm.DrawXYAxis(Graphics g) 在 C:\Users\kasunt\Microsoft Robotics Dev Studio 2008 R3\Marvin\Teleoperation\ MainForm.cs:Victoria.Robotics.Marvin.Teleoperation.MainForm.envMap_Paint(Object sender, PaintEventArgs e) 中的第 2173 行:\Users\kasunt\Microsoft Robotics Dev Studio 2008 R3\Marvin\Teleoperation\MainForm.cs:line 2143在 System.Windows.Forms.Control.OnPaint(PaintEventArgs e) 在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) 在 System.Windows.Forms.Control.WmPaint(Message& m) 在 System .Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control .ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntP tr 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);

【问题讨论】:

  • 从 MainForm.cs 发布你的第 2173 行

标签: c# matrix transform paint invalidoperationexception


【解决方案1】:

这是一个异常,通常是由于在另一个线程中非法使用 Graphics 上下文引起的。我不得不猜测您的程序中还有其他代码也在处理图形。

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

【讨论】:

    【解决方案2】:

    原来是因为下面两行,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 2010-09-19
      • 2010-11-06
      • 2012-02-03
      • 1970-01-01
      • 2011-12-25
      相关资源
      最近更新 更多