C#:为什么绘图速度很慢?
我通过绘制颜色变化的线条来手动绘制线性渐变。然而,这非常慢,当我调整窗口大小时,我似乎会更新。我怎样才能让它更快?在这个例子中,色标是线性的,但后来我不想制作非线性渐变。
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();
}
Im drawing a linear gradient manually by drawing lines with changing colors. However, this is very slow, and i seems to update, when i resize the window. How do i make it faster? The color scale is linear in this example, but later i wan't to make non-linear gradients.
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是 GDI+ 非常慢。
您应该使用相对较快的 GDI+ 高级构造(相对于像现在这样绘制线条)。请参阅 http://msdn.microsoft.com/en- us/library/system.drawing.drawing2d. Lineargradientbrush.aspx 有关例如
LinearGradientBrush
的更多信息。还有更多这样的画笔和笔可以帮助您提高表现。另一件事:
Suspend/ResumeLayout
在您的示例中不执行任何操作。这些方法仅适用于您进行布局时,例如将Control
添加到当前表单或更改现有Control
上的属性(例如Dock
属性)或高度
和宽度
。The problem is that GDI+ is incredibly slow.
You should use high level constructs with GDI+ which are relatively fast (relative to drawing lines like you do now). See http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.lineargradientbrush.aspx for more information about e.g. the
LinearGradientBrush
. There are much more of these brushes and pens which should help you increase your performance.One more thing: the
Suspend/ResumeLayout
doesn't do anything in your example. These methods only apply when you are doing layout by e.g. addingControl
s to the current form or changing properties on existingControl
s like theDock
property or theHeight
andWidth
.如果您只想绘制一次且不调整大小,我建议您将其写入 Bitmap 对象一次,然后将此位图绘制到背景。此外,您还可以在表单上启用双缓冲。这应该是一个名为 DoubleBuffering 的属性,或类似的属性。这应该会减少重绘表单时出现的闪烁。
If you want to paint it once and only once, without resizing, I suggest you write this to a Bitmap object once, and then draw this bitmap to the background. Also, you can enable double buffering on the form. this should be a property called DoubleBuffering, or something similar. This should reduce the flashing you get when redrawing your form.
您可以预先计算颜色值,这样就不必在每次重绘时都执行此操作。除此之外,如果不求助于更底层的 API(例如 XNA),您就无能为力。
更新:在WinForms控件中托管XNA是完全可行的。 这个问题中有一些不错的链接。
You could pre-compute the color values so you won't have to do it on every redraw. Other than that, there's not much more you can do without resorting to more lowlevel APIs, like XNA.
Update: it is perfectly feasible to host XNA within WinForms controls. There's some nice links forward in this question.
也许指定
ColorBlend
与 Pieter 建议的LinearGradientBrush
一起使用将解决您对将来能够绘制非线性渐变的担忧吗?您可以创建一个
ColorBlend
对象来指定您选择的颜色以及每种颜色的任意位置。通过将LinearGradientBrush
的InterpolationColors
属性设置为ColorBlend
对象,您应该能够获得您想要的任何效果。MSDN给出了以下示例:
Perhaps specifying a
ColorBlend
to use with theLinearGradientBrush
suggested by Pieter will address your concerns about being able to paint non-linear gradients in the future?You can create a
ColorBlend
object that specifies the colors of your choice and an arbitrary position for each. By setting theInterpolationColors
property of theLinearGradientBrush
to yourColorBlend
object, you should be able to get any effect that you want.MSDN gives the following sample: