我通过绘制颜色变化的线条来手动绘制线性渐变。但是,这非常慢,而且当我调整窗口大小时,我似乎在更新。我如何让它更快?本例中的色标是线性的,但稍后我不想制作非线性渐变。
protected override void OnPaintBackground(PaintEventArgs paintEvnt)
{
SuspendLayout();
// Get the graphics object
Graphics gfx = paintEvnt.Graphics;
// Create a new pen that we shall use for drawing the line
// Loop and create a horizontal line 10 pixels below the last one
for (int i = 0; i <= 500; i++)
{
Pen myPen = new Pen(Color.FromArgb(i/2,0,0));
gfx.DrawLine(myPen, 0, i, 132, i);
}
ResumeLayout();
}
最佳答案
问题是 GDI+ 非常慢。
您应该使用相对较快的 GDI+ 高级构造(相对于像您现在这样绘制线条)。参见 http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.lineargradientbrush.aspx有关例如的更多信息LinearGradientBrush
。这些画笔和笔的数量更多,应该可以帮助您提高表现。
还有一件事:Suspend/ResumeLayout
在您的示例中没有执行任何操作。这些方法仅在您通过例如进行布局时适用。将 Control
添加到当前表单或更改现有 Control
的属性,例如 Dock
属性或 Height
和宽度
。
关于C#:为什么绘图很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4133896/