Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PictureBox SizeMode设置为缩放时Graphics.DrawLine滞后_C#_Winforms_Drawing - Fatal编程技术网

C# PictureBox SizeMode设置为缩放时Graphics.DrawLine滞后

C# PictureBox SizeMode设置为缩放时Graphics.DrawLine滞后,c#,winforms,drawing,C#,Winforms,Drawing,我有一个winforms应用程序,我在图片盒中绘制图像。绘图部分工作得非常好,除了当PictureBox.SizeMode=PictureBoxSizeMode.Zoom时的滞后。我将SizeMode设置为Zoom的原因是,我可以在保留内存的同时放大和缩小图像。有没有办法加快绘图过程 PictureBox的绘制方法的代码如下所示: pen = new Pen(color, 5); solidBrush = new SolidBrush(solid); e.Graphics.SmoothingMo

我有一个winforms应用程序,我在图片盒中绘制图像。绘图部分工作得非常好,除了当
PictureBox.SizeMode=PictureBoxSizeMode.Zoom
时的滞后。我将SizeMode设置为Zoom的原因是,我可以在保留内存的同时放大和缩小图像。有没有办法加快绘图过程

PictureBox的绘制方法的代码如下所示:

pen = new Pen(color, 5);
solidBrush = new SolidBrush(solid);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.ScaleTransform(PictureScale, PictureScale);

foreach (List<Point> polygon in Polygons)
{
  e.Graphics.DrawLines(pen, polygon.ToArray());
}

if (NewPolygon != null)
{
  Pen pp = new Pen(color, 5);
  if (NewPolygon.Count > 1)
  {
    e.Graphics.DrawLines(pp, NewPolygon.ToArray());
  }
  if (NewPolygon.Count > 0)
  {
    using (Pen dashed_pen = pp)
    {
      dashed_pen.DashPattern = new float[] { 3, 3 };
      e.Graphics.DrawLine(dashed_pen, NewPolygon[NewPolygon.Count - 1], NewPoint);
    }
  }
}

这里有丢失的碎片。滞后似乎是由计算当前鼠标指针位置并使控件无效的过程生成的。IMO,你应该展示这一部分(也许你正在使用计时器)。也不清楚为什么会有
if(NewPolygon.Count>1)
和紧跟在
if(NewPolygon.Count>0)
之后的
if(NewPolygon.Count>0)
,它也与之前的条件匹配(还因为
NewPolygon
应该保持新线坐标)。那么,
NewPoint
在这里做什么?它不是
NewPolygon
中的最后一个点吗?
solidBrush
在这里设置,但它似乎是一个字段或属性,就像前面的画笔一样。如果这些是字段,请将其更改为属性,并在窗体关闭时将其处理掉。在原地创建的另一个对象,也在原地处置。SolidBrush似乎用于绘制点。也发布代码,这样可以清楚地看到点和线是如何同步的;我不在那里。我还添加了一些代码,以使其更清晰您发布的代码不会产生任何结果。给我们重现问题的代码。我以完整的形式添加了代码,变量
NewPolygon、polygon、NewPoint、pen、color和scaffolds
都是全局变量
    int x = 0;
    int y = 0;
    bool drag = false;
    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {

        Point spot = new Point((int)((float)(e.Location.X) / PictureScale), (int)((float)(e.Location.Y) / PictureScale));
        if (e.Button == MouseButtons.Middle)
        {
            x = e.X;
            y = e.Y;
            drag = true;
        }
        else
        {
                if (NewPolygon != null)
                {
                    if (e.Button == MouseButtons.Right)
                    {
                        if (NewPolygon.Count > 1)
                        {
                            int counter = NewPolygon.Count;
                            Polygons.Add(NewPolygon);
                        }
                        NewPolygon = null;
                    }
                    else if (e.Button == MouseButtons.Left)
                    {

                        if (NewPolygon[NewPolygon.Count - 1] != spot)
                        {
                            NewPolygon.Add(spot);
                            scaffolds.Add(new Rectangle(spot.X - 3, spot.Y - 3, 6, 6));
                        }
                    }
                }
                else
                {
                    NewPolygon = new List<Point>();
                    NewPoint = spot;
                    NewPolygon.Add(spot);
                    scaffolds.Add(new Rectangle(spot.X - 3, spot.Y - 3, 6, 6));
                }
            }
        //this.Capture = true;
        pictureBox1.Refresh();
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {

        if (drag)
        {
            pictureBox1.Top += e.Y - y;
            pictureBox1.Left += e.X - x;
            this.Cursor = Cursors.SizeAll;
        }
        else
        {
            if (NewPolygon == null)
                return;
            NewPoint = new Point((int)((float)(e.Location.X) / PictureScale), (int)((float)(e.Location.Y) / PictureScale));
            pictureBox1.Refresh();
        }

    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.ScaleTransform(PictureScale, PictureScale);

        foreach (Rectangle rect in scaffolds)
        {
            e.Graphics.DrawEllipse(pen, rect);
        }

        foreach (List<Point> polygon in Polygons)
        {
            e.Graphics.DrawLines(pen, polygon.ToArray());
        }

        if (NewPolygon != null)
        {
            Pen pp = new Pen(color, 5);

            if (NewPolygon.Count > 0)
            {
                if (NewPolygon.Count > 1)
                {
                    e.Graphics.DrawLines(pp, NewPolygon.ToArray());
                }
                using (Pen dashed_pen = pp)
                {
                    dashed_pen.DashPattern = new float[] { 3, 3 };
                    e.Graphics.DrawLine(dashed_pen, NewPolygon[NewPolygon.Count - 1], NewPoint);
                }

            }
        }

    }