简体   繁体   English

C#同时绘制多个圆圈

[英]c# draw many circle at the same time

I tried to build my own Polar Chart . 我试图建立自己的极坐标图 I use a function to draw points as follows: 我使用一个函数来绘制点,如下所示:

private void drawPoints()
{
    this.SuspendLayout();
    Graphics g = this.CreateGraphics();

    int i = 0;
    foreach (Point Pointaktuell in PointList)
    {
        int radius = 15;

        Brush b = new SolidBrush(Color.Red);

        g.FillEllipse(b, (int)(Pointaktuell.X - radius / 2.0), (int)(Pointaktuell.Y - radius / 2.0), radius, radius);
        i++;
    }
    PointList.Clear();
    this.ResumeLayout();
}

The problem is that the points are draw one after one, and need a lot of time. 问题在于要点一一画完,需要很多时间。 How can I draw them all at once? 我如何一次绘制它们?

To optimize the drawing procedure, try put as more code as you can outside the loop. 为了优化绘制过程中,尽量把更多的代码,你可以在循环 Do not forget to close IDisposable 别忘了关闭IDisposable

private void drawPoints() 
{
    SuspendLayout();

    try 
    {
        using (Graphics g = CreateGraphics()) 
        {
            int i = 0; 
            int radius = 15;

            using (b = new SolidBrush(Color.Red)) 
            {
                foreach (Point Pointaktuell in PointList) 
                {
                    g.FillEllipse(b, (int)(Pointaktuell.X - radius / 2.0), (int)(Pointaktuell.Y - radius / 2.0), radius, radius);
                    i += 1;
                }
            }
        }
   }
   finally { ResumeLayout(); }
}

A new SolidBrush instantiated in each loop step. 在每个循环步骤中实例化一个新的SolidBrush And radius is redefined every time again and again. radius一次又一次地重新定义。 Dispose() routine is not called for Instantiated objects (which comes to be important for Graphics ) 实例化对象不调用Dispose()例程(这对于Graphics很重要)

You might try the following; 您可以尝试以下方法;

private void drawPoints()
{
    this.SuspendLayout();

    const int radius = 15;
    using (Graphics g = this.CreateGraphics())
    {
        //if (g == null) { this.ResumeLayout(); return; } // # Uncomment this line if you want defensive checks
        using (Brush b = new SolidBrush(Color.Red))
        {
            for (int i = 0; i < PointList.Count; i++)
            {
                g.FillEllipse(b, (int)(PointList[i].X - radius / 2.0), (int)(PointList[i].Y - radius / 2.0), radius, radius);
            }
        }
    }

    PointList.Clear();
    this.ResumeLayout();
}

This is only if you'r going to do all drawing stuff in drawPoints() and call it once in a time. 仅当您要在drawPoints()完成所有绘图工作并一次调用一次时。 If you'r going to call drawPoints() periodically or have more drawing routines in other parts of the flow; 如果您要定期调用drawPoints()或在流程的其他部分使用更多绘图例程; i suggest you keep that Graphics g and Brush b referances globally and Dispose() when they are no longer needed (no more drawing in this case) 我建议您在不再需要Graphics gBrush b引用以及Dispose()时保留它们(在这种情况下不再绘制)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM