该程序退出代码为1073741819(0xc0000005)'访问冲突'

时间:2016-06-20 06:30:40

标签: c# graphics path draw

我使用graphicPath在panel1中绘制点和线。代码如下:

private void panel1_Paint_1(object sender, PaintEventArgs e)
    {                 
        Graphics G = e.Graphics;
        GraphicsPath gp = new GraphicsPath();

        foreach (var line in tockeKoordinate)
        {
            gp.AddLine((float) (line.startX), (float) (line.startY), (float) (line.endX), (float) (line.endY));
            gp.CloseFigure();
        }
        var rect = gp.GetBounds();
        var scale = Math.Min(1f * (int)(panel1.ClientSize.Width) / rect.Width,
                               1f * (int)(panel1.ClientSize.Height) / rect.Height);          

        using (Pen pen = new Pen(Color.Black, 0.0001f))
                {
                    G.SmoothingMode = SmoothingMode.AntiAlias;
                    G.Clear(Color.White);
                    G.TranslateTransform(0, +panel1.ClientSize.Height);
                    G.ScaleTransform(scale, -scale);
                    G.TranslateTransform(-rect.X, -rect.Y);
                    G.DrawPath(pen, gp);
               }
        if(checkBox1.Checked)
        {
            gp.ClearMarkers();
            foreach (var line2 in tockeZelene)
            {
                gp.AddLine((float)(line2.startX), (float)(line2.startY), (float)(line2.endX), (float)(line2.endY));
                gp.CloseFigure();
            }

            using (pen2);
            {
                G.DrawPath(pen2, gp);   <--- access violation here
            }  
        }      
    }

基本上我有两个ListsTockekoordinatetockeZelena。第一个包含所有点,第二个包含第一个点的约30%,我想用我的pen2绘制绿色,这是在开始时初始化。

假设选中checkbox1,我会运行所有点以获得矩形GetBounds,因此我可以使用点坐标缩放panel1。

然后checkbox1.checked部分出现,应用程序退出标记的行。

有谁知道这会导致什么?或者至少知道如何设置VS以显示有关所述错误的更多信息?

1 个答案:

答案 0 :(得分:1)

以下这一行有点可疑..

 using (pen2); //<--this one!!!
 {
      G.DrawPath(pen2, gp);
 }

首先,DrawPath将始终抛出异常,因为您将使用已处置的对象。要解决这个问题,请删除分号......

using (pen2)
 {
      G.DrawPath(pen2, gp);
 }

其次,pen2是什么?谁在使用它?如果它被另一个线程使用,则会发生访问冲突,因为pen2的使用不是线程安全的。

最后,不要在Paint事件中处置全局对象(pen2),除非您一直在重新创建它,因为每次控件需要重绘其表面时都会触发此事件。这意味着,第二次您的控件需要重绘时,它将使用已处置的对象。

相关问题