有没有办法让创建特定大小的点的方法更快?

时间:2016-12-23 03:48:30

标签: c# .net winforms

该方法是在一个新类中,我从form1使用它: 在新课程的顶部:

public int numberOfPoints = 100;

类Init方法:

public void Init()
        {
if (IsEmpty(bmpWithPoints) == true)
            {
                bmpWithPoints = GetBitmapWithEllipses(1.0f);
            }
        }

然后方法:

private Bitmap GetBitmapWithEllipses(float radius)
        {
            Bitmap bmp = new Bitmap(512, 512);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.Black);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                //store the Ellipses in a GraphicsPath
                using (System.Drawing.Drawing2D.GraphicsPath gP = new System.Drawing.Drawing2D.GraphicsPath())
                {
                    for (int x = 0; x < numberOfPoints; x++)
                    {
                        for (int y = 0; y < numberOfPoints; y++)
                        {
                            Color c = Color.FromArgb(
                                r.Next(0, 256),
                                r.Next(0, 256),
                                r.Next(0, 256));

                            using (SolidBrush sb = new SolidBrush(c))
                            {
                                Point pt = new Point(r.Next(bmp.Width), r.Next(bmp.Height));

                                //clone and widen the path to determine, whether the new point overlaps
                                using (System.Drawing.Drawing2D.GraphicsPath gP2 = (System.Drawing.Drawing2D.GraphicsPath)gP.Clone())
                                {
                                    using (Pen pen = new Pen(Brushes.White, radius))
                                    {
                                        gP2.Widen(pen);
                                        while (gP2.IsVisible(pt.X, pt.Y) || gP2.IsOutlineVisible(pt, pen))
                                        {
                                            pt = new Point(r.Next(bmp.Width), r.Next(bmp.Height));
                                        }
                                    }
                                }

                                RectangleF rc = new RectangleF(pt.X - radius, pt.Y - radius, radius * 2, radius * 2);
                                g.FillEllipse(sb, rc);
                                gP.StartFigure();
                                gP.AddEllipse(rc);
                                gP.CloseFigure();
                            }
                        }
                    }
                }
            }

            return bmp;
        }

在Form1中:

public Form1()
        {
            InitializeComponent();

            textBox1.Text = trackBar1.Maximum.ToString();
            de.pb1 = pictureBox1;

            de.bmpWithPoints = new Bitmap(512, 512);
            de.numberOfPoints = 20;
            de.randomPointsColors = true;
            de.Init();
        }

例如,当我将form1中的点数设置为10时,它将快速运行该方法并快速返回bmp 10点。 但是如果我在form1中将点数设置为20,则需要大约3-5秒。 如果我在form1中将它设置为50分,则需要几分钟。

  1. 限制最大点数应该是多少?图像的大小是512,512。因此,它的逻辑i将能够用用户表示逻辑的512 * 512 = 262144的点填充所有图像。用户可以设置点数和大小。在这个例子中,每个点的半径为1.0。

  2. 如何使GetBitmapWithEllipses方法更快地创建点?

1 个答案:

答案 0 :(得分:0)

这就是它的工作原理^^哦,你创造的东西比我想的要多。 如果输入10将创建100.如果输入4,则得到16. numberof points * numberofpoints

无论如何这里是我想要的(如果你想要输入的确切点数,请删除一个循环)^^

    private Bitmap GetBitmapWithEllipses(float radius)
    {
        Bitmap bmp = new Bitmap(512, 512);
        Random r = new Random();

        using (Graphics g = Graphics.FromImage(bmp))
        {
            g.Clear(Color.Black);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            List<RectangleF> rects = new List<RectangleF>();
            rects.Add(new RectangleF(0, 0, 0, 0));

            for (int x = 0; x < numberOfPoints; x++)
            {
                for (int y = 0; y < numberOfPoints; y++)
                {
                    Color c = Color.FromArgb(
                        r.Next(0, 256),
                        r.Next(0, 256),
                        r.Next(0, 256));

                    PointF p = new PointF(r.Next(Width), r.Next(Height));
                    RectangleF rect = new RectangleF(p, new SizeF(radius * 2, radius * 2));

                    if (!rects.Any(tmp => tmp.IntersectsWith(rect)))
                    {
                        rects.Add(rect);
                        g.FillEllipse(new SolidBrush(c), rect);
                    }
                    else
                    {
                        y--;
                    }
                }
            }
        }

        return bmp;
    }