我怎样才能在滚动的WinForms摆脱动画抽筋的? [英] How can I get rid of jerkiness in WinForms scrolling animation?

查看:220
本文介绍了我怎样才能在滚动的WinForms摆脱动画抽筋的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中的简单控件,就像一个图片框,除了形象不断向上滚动(并从底部重新出现)。动画效果由计时器(System.Threading.Timer)驱动,从缓存的图像副本(两部分),以隐藏的缓冲器中,然后在其Paint事件绘制到控制的表面

I'm writing a simple control in C# that works like a picture box, except the image is constantly scrolling upwards (and re-appearing from the bottom). The animation effect is driven by a timer (System.Threading.Timer) which copies from the cached image (in two parts) to a hidden buffer, which is then drawn to the control's surface in its Paint event.

的问题是,当以每秒20+帧的高帧速率运行该滚动动画效果稍微干(在较低的帧速率的效果太小而被感知)。我怀疑这急动是因为动画不符合我的显示器的刷新率,这意味着每一帧图像在屏幕的时间变长的,而不是整整25毫秒的任何方式进行同步。

The problem is that this scrolling animation effect is slightly jerky when run at a high frame rate of 20+ frames per second (at lower frame rates the effect is too small to be perceived). I suspect that this jerkiness is because the animation is not synchronized in any way with my monitor's refresh rate, which means that each frame stays on the screen for a variable length of time instead of for exactly 25 milliseconds.

有没有什么方法可以让我得到这个动画平滑滚动?

Is there any way I can get this animation to scroll smoothly?

您可以下载一个示例应用程序这里(运行它,点击开始),和源$ C ​​$ c是这里。它看起来并不可怕生涩,但如果你看它观察你可以看到打嗝。

You can download a sample application here (run it and click "start"), and the source code is here. It doesn't look horribly jerky, but if you look at it closely you can see the hiccups.

警告:这个动画产生了pretty怪异的错觉效应可能让你有点难受。如果你看它一会儿,然后把它关掉,它看起来好像你的屏幕垂直拉伸本身。

WARNING: this animation produces a pretty weird optical illusion effect which might make you a little sick. If you watch it for awhile and then turn it off, it will look as if your screen is stretching itself vertically.

更新:作为一个实验,我试着与我的滚动位图创建一个AVI文件。其结果是比我的WinForms动画生涩少了,但还是不能接受的(它仍然让我恶心到我的肚子看太久)。我想,我遇到的不是刷新率正在同步一个根本性的问题,所以我可能要坚持让生病的人与我的长相和性格。

UPDATE: as an experiment, I tried creating an AVI file with my scrolling bitmaps. The result was less jerky than my WinForms animation, but still unacceptable (and it still made me sick to my stomach to watch it for too long). I think I'm running into a fundamental problem of not being synced with the refresh rate, so I may have to stick to making people sick with my looks and personality.

推荐答案

您将需要等待的 VSYNC你绘制缓冲图像之前的。

有一个 $ C $的CProject文章,那建议使用多媒体计时器和DirectX的方法 的IDirectDraw :: GetScanLine()

There is a CodeProject article that suggests using a multimedia timer and DirectX' method IDirectDraw::GetScanLine().

我敢肯定,你可以通过托管DirectX使用方法从C#。

I'm quite sure you can use that method via Managed DirectX from C#.

编辑:

在一些更多的研究和谷歌上搜索我得出的结论是通过GDI绘图不会发生实时即使你在准确适当的时候拉它实际上可能为时已晚发生,你将有撕裂的结论。

After some more research and googling I come to the conclusion that drawing via GDI doesn't happen in realtime and even if you're drawing in the exact right moment it might actually happen too late and you will have tearing.

所以,用GDI这似乎不是可能的。

So, with GDI this seems not to be possible.

这篇关于我怎样才能在滚动的WinForms摆脱动画抽筋的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆