4

最も簡単な方法でアニメーション GIF を表示する WinForms アプリがあります。.gif を直接読み込む PictureBox があります。

WinForms デザイナーによって生成されたコードは次のようになります。

        // 
        // pictureBoxHomer
        // 
        this.pictureBoxHomer.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
        this.pictureBoxHomer.Dock = System.Windows.Forms.DockStyle.Fill;
        this.pictureBoxHomer.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxHomer.Image")));
        this.pictureBoxHomer.Location = new System.Drawing.Point(3, 3);
        this.pictureBoxHomer.Name = "pictureBoxHomer";
        this.pictureBoxHomer.Size = new System.Drawing.Size(905, 321);
        this.pictureBoxHomer.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
        this.pictureBoxHomer.TabIndex = 0;
        this.pictureBoxHomer.TabStop = false;

画像はもちろんこれです: http://media.tumblr.com/tumblr_m1di1xvwTe1qz97bf.gif

問題: このアニメーション gif はブラウザーでは驚くほど表示されますが、WinForms アプリでは実行速度が速すぎて、必要なほど快適ではありません。そう:

質問: WinForms アプリでアニメーション GIF を遅くする方法はありますか?

4

2 に答える 2

4

答えは、C# よりも画像に関連していると思います。その特定の画像をGIMPなどのツールで編集してレイヤーを確認すると、10レイヤー(フレーム)の構成であることがわかりますが、レイヤー間の「遅延時間」は設定されていません-レイヤーの属性。レイヤーの属性を編集し、それを右クリックしてメニューでそのオプションを選択することで変更できます。もちろん、最後に新しい画像をエクスポートして GIF として保存し、オプションで「アニメーション」を選択する必要があります。

この場合(フレーム間の遅延時間が指定されていない場合)、WebブラウザーとC#PicutureBoxは独自の異なるデフォルト値を強制すると思います。したがって、ここで手順 3 で説明したように、たとえば 100 ミリ秒の遅延を設定すると、アニメーションが遅くなります。

于 2012-08-20T19:52:11.037 に答える
0

今後の参考のために、画像ボックス内の GIF の遅延時間をオーバーライドすることができます。大まかな例を次に示します。

    public partial class Form1 : Form
{
    private FrameDimension dimension;
    private int frameCount;
    private int indexToPaint;
    private Timer timer = new Timer();

    public Form1()
    {
        InitializeComponent();

        dimension = new FrameDimension(this.pictureBox1.Image.FrameDimensionsList[0]);
        frameCount = this.pictureBox1.Image.GetFrameCount(dimension);
        this.pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint); 
        timer.Interval = 100;
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }

    void timer_Tick(object sender, EventArgs e)
    {
        indexToPaint++;
        if(indexToPaint >= frameCount)
        {
            indexToPaint = 0;
        }
    }

    void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        this.pictureBox1.Image.SelectActiveFrame(dimension, indexToPaint);
        e.Graphics.DrawImage(this.pictureBox1.Image, Point.Empty);
    }
}
于 2012-08-20T20:53:38.317 に答える