【发布时间】:2019-08-28 13:55:50
【问题描述】:
我目前正在开发一个 C# 应用程序,该应用程序使用 Drawing.Graphics 绘制到我在图片框中显示的位图。我正在渲染一个带有网格的图形,所以除了屏幕上的一些点之外,我还必须绘制大约 200 条网格线。我可以滚动以在图表中移动,但在这样做的同时,我注意到我的绘图功能表现不佳,因为它结结巴巴。用 OpenTK 和它的 GLControl 类替换这种方法使得渲染非常高效,滚动非常流畅。
有什么方法可以让 Drawing.Graphics 获得更快的性能?
// This is only done when the window is resized
Bitmap bmp = new Bitmap(picRender.Width, picRender.Height);
Graphics g = Graphics.FromImage(bmp);
// Drawing code
g.Clear(picRender.BackColor);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
if (background != null)
{
g.DrawImage(background, new Rectangle(0, 0, picRender.Width, picRender.Height));
}
for (int n = 0; n < trajectories.Count; n++)
{
Trajectory trajectory = trajectories[n];
Pen pen = new Pen(new SolidBrush(TrajectoryColors[n % TrajectoryColors.Length]));
if (RenderLines)
{
for (int i = 0; i < trajectory.samples.Count - 1; i++)
{
if (Interval_Start > trajectory.samples[i + 1].time ||
Interval_End < trajectory.samples[i].time) { continue; }
g.DrawLine(pen, coordinate_system.ToPoint(trajectory.samples[i].coordinates),
coordinate_system.ToPoint(trajectory.samples[i + 1].coordinates));
}
}
}
// End of drawing
【问题讨论】:
-
@Dmi,你能显示一些代码吗?性能可能取决于您采用的方法,因此拥有该类型上下文会有所帮助。
-
从位图创建图形,DrawLine x200,处理图形。 :)
-
我曾经为 Windows Phone 7 制作了 System.Drawing.Graphics 的硬件加速版本。虽然这不是最好的方法;除非你有充分的理由重写 GDI+。 OpenTK 的图形处理方法很快,因为它使用 GPU 加速。
-
@Dmi:向我们展示代码。它可能是 System.Graphics,也可能是您的代码。在我看到你的代码之前,我必须假设它是你的代码。
-
您在抱怨滚动性能,此代码在滚动期间不运行。以 Format32bppPArgb 像素格式创建位图,它的渲染速度比其他任何格式都快 10 倍。