WPF 画线测试

这篇博客通过对比Winform和WPF两个平台下使用图形API进行线条绘制的示例,探讨了它们的渲染速度和性能。在Winform中,使用Graphics对象和BufferedGraphics进行绘制,而在WPF中则利用DrawingVisual进行绘制。博客中包含详细的代码实现,并通过计数器记录帧率,展示了10秒内两种方式的绘制次数和速率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Winform

using System;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;

namespace MyNamespace
{
public class MyForm : Form
{
private static readonly Pen[] pens = new[] {
Pens.Red,
Pens.Green,
Pens.Blue,
Pens.Cyan,
Pens.Yellow,
Pens.Magenta,
};

private static readonly Random random = new Random();

private const int LINE_WIDTH = 800;
private volatile bool isRendering = true;
private int frameCount = 0;

private Graphics graphics = null;
private BufferedGraphics gfx = null;

public MyForm()
{
Width = LINE_WIDTH;
Height = LINE_WIDTH;
StartPosition = FormStartPosition.CenterScreen;

gfx = BufferedGraphicsManager.Current.Allocate(CreateGra phics(),
new Rectangle(0, 0, LINE_WIDTH, LINE_WIDTH));
graphics = gfx.Graphics;

Shown += (o, e) => TestRenderingSpeed();
}

private void TestRenderingSpeed()
{
Console.Write(String.Format("Run for 10 seconds... "));

new Thread(() =>
{
Thread.Sleep(10000);
isRendering = false;
}).Start();

int totalPens = pens.Length;

while (isRendering)
{
DrawLines(graphics, pens[random.Next(totalPens)],
pens[random.Next(totalPens)]);
gfx.Render();
}

Console.WriteLine(String.Format("Finished.\nFrame count: {0}
Rate: {1} fps", frameCount, frameCount / 10));
}

private void DrawLines(Graphics g, Pen pen1, Pen pen2)
{
g.FillRectangle(Brushes.Black, 0, 0, LINE_WIDTH, LINE_WIDTH);

for (int x = 0; x < LINE_WIDTH; x += 3)
{
g.DrawLine(pen1, x, 0, x, LINE_WIDTH);
}

for (int y = 0; y < LINE_WIDTH; y += 3)
{
g.DrawLine(pen2, 0, y, LINE_WIDTH, y);
}

frameCount++;
}
}

public class MyClass
{
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MyForm());
}
}
}

2. WPF

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using System.Threading;

namespace MyNamespace
{
public class MyWindow : Window
{
private static readonly Pen[] pens = new[] {
new Pen(Brushes.Red, 1.0),
new Pen(Brushes.Green, 1.0),
new Pen(Brushes.Blue, 1.0),
new Pen(Brushes.Cyan, 1.0),
new Pen(Brushes.Yellow, 1.0),
new Pen(Brushes.Magenta, 1.0),
};

private static readonly Random random = new Random();

private MyDrawing drawing;
private const int LINE_WIDTH = 800;
private volatile bool isRendering = true;

public MyWindow()
{
Width = LINE_WIDTH;
Height = LINE_WIDTH;
WindowStartupLocation = WindowStartupLocation.CenterScreen;

drawing = new MyDrawing(LINE_WIDTH);
Content = drawing;

Loaded += (o, e) => TestRenderingSpeed();
}

private void TestRenderingSpeed()
{
Console.Write(String.Format("Run for 10 seconds... "));

new Thread(() =>
{
Thread.Sleep(10000);
isRendering = false;
}).Start();

int totalPens = pens.Length;

while (isRendering)
{
drawing.DrawLines(pens[random.Next(totalPens)],
pens[random.Next(totalPens)]);
}

Console.WriteLine(String.Format("Finished.\nFrame count: {0}
Rate: {1} fps", drawing.FrameCount, drawing.FrameCount / 10));
}
}

public class MyDrawing : FrameworkElement
{
private DrawingVisual visual;
private readonly int lineWidth;
private int frameCount = 0;

public int FrameCount
{
get { return frameCount; }
}

public MyDrawing(int lineWidth)
{
this.lineWidth = lineWidth;
visual = new DrawingVisual();
}

public void DrawLines(Pen pen1, Pen pen2)
{
using (DrawingContext dc = visual.RenderOpen())
{
for (int x = 0; x < lineWidth; x += 3)
{
dc.DrawLine(pen1, new Point(x, 0), new Point(x, lineWidth));
}

for (int y = 0; y < lineWidth; y += 3)
{
dc.DrawLine(pen2, new Point(0, y), new Point(lineWidth, y));
}
}

frameCount++;
}

protected override int VisualChildrenCount
{
get { return 1; }
}

protected override Visual GetVisualChild(int index)
{
return visual;
}
}

public class MyClass
{
[STAThread]
public static void Main(string[] args)
{
Application app = new Application();
app.Run(new MyWindow());
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值