- 主题:c# 绘制类似心电图的实时波形如何才能不闪啊?
在form里加了定时器50ms refrsh一次,结果发现屏幕闪烁很厉害,10ms刷新也一样
OnPaint里绘图代码如下,已经用了bmp贴图模式
Bitmap bmp = new Bitmap(1024, 768);
Graphics g = Graphics.FromImage(bmp);
Pen pen = new Pen(Color.Red);
int i;
for(i=0;i<myWave.Length-1;i++) {
g.DrawLine(pen, i, myWave[i], i+1, myWave[i+1]);
}
this.CreateGraphics().DrawImage(bmp, 0, 0);
pen.Dispose();
g.Dispose();
--
FROM 114.214.197.*
试试启用双buffer
https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-reduce-graphics-flicker-with-double-buffering-for-forms-and-controls?view=netframeworkdesktop-4.8
【 在 wjie 的大作中提到: 】
: 在form里加了定时器50ms refrsh一次,结果发现屏幕闪烁很厉害,10ms刷新也一样
: OnPaint里绘图代码如下,已经用了bmp贴图模式
: Bitmap bmp = new Bitmap(1024, 768);
: ...................
--
FROM 113.66.231.*
我这边试了一点闪烁都没有啊
using Timer = System.Threading.Timer;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
this.DoubleBuffered = true;
InitializeComponent();
SetWavesValues();
timer1 = new Timer(RefreshWaves, null, TimeSpan.FromMilliseconds(50), TimeSpan.FromMilliseconds(50));
}
private Timer timer1;
private int[] myWave = new int[1024];
void RefreshWaves(object? o)
{
try
{
SetWavesValues();
this.Invoke(this.Refresh);
}
catch { }
}
void SetWavesValues()
{
for (int i = 0; i < myWave.Length; i++)
{
myWave[i] = Random.Shared.Next(768);
}
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
using Bitmap bmp = new Bitmap(1024, 768);
using Graphics g = Graphics.FromImage(bmp);
using Pen pen = new Pen(Color.YellowGreen);
int i;
for (i = 0; i < myWave.Length - 1; i++)
{
g.DrawLine(pen, i, myWave[i], i + 1, myWave[i + 1]);
}
e.Graphics.DrawImage(bmp, 0, 0);
}
}
}
【 在 wjie 的大作中提到: 】
: 这个在创建form的时候已经使能了,但还是闪,无论是1ms还是100ms定时刷新都闪,感觉好像CPU忙不过来的样子
: this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |ControlStyles.AllPaintingInWmPaint, true);
: this.UpdateStyles();
: ...................
--
FROM 113.66.230.*
我明白了,你的问题是自己创建了 Graphics
this.CreateGraphics() // root cause
这导致框架管理的 double buffering 不生效
直接用 e.Graphics
【 在 wjie 的大作中提到: 】
: 这个在创建form的时候已经使能了,但还是闪,无论是1ms还是100ms定时刷新都闪,感觉好像CPU忙不过来的样子
: this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |ControlStyles.AllPaintingInWmPaint, true);
: this.UpdateStyles();
: ...................
--
FROM 113.66.230.*
多谢高人,一语中的!
【 在 keygen 的大作中提到: 】
: 我明白了,你的问题是自己创建了 Graphics
: this.CreateGraphics() // root cause
: 这导致框架管理的 double buffering 不生效
: ...................
--
FROM 114.214.197.*
scottplot
【 在 wjie 的大作中提到: 】
: 在form里加了定时器50ms refrsh一次,结果发现屏幕闪烁很厉害,10ms刷新也一样
: OnPaint里绘图代码如下,已经用了bmp贴图模式
: Bitmap bmp = new Bitmap(1024, 768);
: ...................
--
FROM 101.229.80.*
就像这种困扰了我好多年的绝世好信息,一般从哪儿获得....
【 在 keygen 的大作中提到: 】
: 我明白了,你的问题是自己创建了 Graphics
: this.CreateGraphics() // root cause
: 这导致框架管理的 double buffering 不生效
: ...................
--
FROM 222.131.10.*
我估计你如果跟着官方文档来写 repaint,应该没什么机会错。。
不过我也没看倒是
【 在 moneybox 的大作中提到: 】
: 就像这种困扰了我好多年的绝世好信息,一般从哪儿获得....
--
FROM 14.145.21.*