我有一个winforms
应用程序,上面有一个 gif,用于让用户了解停滞的进程。
问题是它的播放速度比其他应用程序(chrome、internet explorer)慢得多。
我已经尝试过 gif 了PictureBox
and Label
但结果速度是相同的。经过一番研究后我发现this问题和传奇@Hans Passant的答案,但不幸的是应用他建议的样板代码没有任何区别。
下面是简单的重现代码:
public partial class Form1 : Form
{
public Form1 ()
{
InitializeComponent();
timeBeginPeriod(timerAccuracy);
}
protected override void OnFormClosed ( FormClosedEventArgs e )
{
timeEndPeriod(timerAccuracy);
base.OnFormClosed(e);
}
// Pinvoke:
private const int timerAccuracy = 10;
[System.Runtime.InteropServices.DllImport("winmm.dll")]
private static extern int timeBeginPeriod ( int msec );
[System.Runtime.InteropServices.DllImport("winmm.dll")]
public static extern int timeEndPeriod ( int msec );
}
如果需要的话,还有设计师代码:
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose ( bool disposing )
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent ()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(8, 9);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(166, 119);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// label1
//
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.label1.Image = ((System.Drawing.Image)(resources.GetObject("label1.Image")));
this.label1.Location = new System.Drawing.Point(180, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(158, 119);
this.label1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(346, 134);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label1;
}
两个 gif 的播放速度相同,但低于实际的 gif。应用此代码时还有其他需要注意的地方吗?
你只能得到猜测,我怀疑任何人都会有运气得到复制品:
- timeBeginPeriod() 从技术上讲可能会失败,尽管当您要求 10 毫秒时这种情况很不寻常,请验证它是否返回 0。
- 如果图像很大,那么它可能无法足够快地更新。或者你的 UI 线程被其他职责占用太多。 gif 的像素格式与现代机器上视频适配器的像素格式不匹配。每次更新框架时都会完成转换。这是相当昂贵的,特别是如果您还强制重新缩放图像(即 PictureBox.SizeMode != Normal)。使用任务管理器验证您的 UI 线程没有消耗 100% 的核心。
- 您可以通过运行获得有关有效计时器周期的第二意见
powercfg /energy
从提升的命令提示符。在您的应用程序运行时执行此操作。它将运行一分钟,然后生成一个 HTML 文件,您可以使用浏览器查看该文件。在“平台计时器解析:计时器请求堆栈”标题下报告,请求的周期值应为 10000。请注意其他进程或驱动程序也可能已发出请求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)