我如何更有效地画画?

时间:2021-06-20 21:29:48

How do i draw this more efficiently?

我如何更有效地绘制这个?

I can feel the lag when i call the code below. NOTE this is about pixel editing and not clearing the screen.

当我调用下面的代码时,我会感觉到滞后。注意这是关于像素编辑而不是清除屏幕。

    int colorIndex = 0;
    private void pictureBox1_Click(object sender, EventArgs e)
    {
        if (colorIndex == 0)
            draw(Color.DimGray);
        else if(colorIndex ==1)
            draw(Color.ForestGreen);
        colorIndex++;
        colorIndex = colorIndex % 2;

        pictureBox1.Invalidate();
        //pictureBox1.Update();
    }

    void draw(Color c)
    {
        //var bdata = b.LockBits(Rectangle.Empty, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        //var px = bdata.Scan0;
        var px = b;
        {
            for (int y = 0; y < b.Height; y++)
            {
                for (int x = 0; x < b.Width; x++)
                    //px[y * b.Width + x] = -1;
                    px.SetPixel(x, y, c);
            }
        }
        //b.UnlockBits(bdata);
    }

3 个解决方案

#1


Have you enable double buffering?

你启用了双缓冲吗?

btw, If you are just drawing rectangle, you can use the DrawRectangle method.

顺便说一句,如果你只是绘制矩形,你可以使用DrawRectangle方法。

#2


How about:

void draw(Color c) {
   using (Graphics g = Graphics.FromImage(b)) {
      g.Clear(c);
   }
}

#3


SetPixel/GetPixel are generally slow operations. If you can use unsafe code (code which uses pointers), there are faster methods of access, but they're slightly more involved. There is a tutorial here which explains how it works, however:

SetPixel / GetPixel通常运行缓慢。如果你可以使用不安全的代码(使用指针的代码),有更快的访问方法,但它们涉及的更多。这里有一个教程解释它是如何工作的,但是:

http://www.codeproject.com/KB/GDI-plus/csharpgraphicfilters11.aspx

#1


Have you enable double buffering?

你启用了双缓冲吗?

btw, If you are just drawing rectangle, you can use the DrawRectangle method.

顺便说一句,如果你只是绘制矩形,你可以使用DrawRectangle方法。

#2


How about:

void draw(Color c) {
   using (Graphics g = Graphics.FromImage(b)) {
      g.Clear(c);
   }
}

#3


SetPixel/GetPixel are generally slow operations. If you can use unsafe code (code which uses pointers), there are faster methods of access, but they're slightly more involved. There is a tutorial here which explains how it works, however:

SetPixel / GetPixel通常运行缓慢。如果你可以使用不安全的代码(使用指针的代码),有更快的访问方法,但它们涉及的更多。这里有一个教程解释它是如何工作的,但是:

http://www.codeproject.com/KB/GDI-plus/csharpgraphicfilters11.aspx