
C#でのアニメーション再生・停止方法についてご紹介!
- SE
- C#でのアニメーションがうまく動かせなくて困っています。
- PM
- なるほど。ではアニメーションgifを使った再生・停止方法をみていきましょう。
目次
C#でのアニメーションについて
今回は、C#を使ったアニメーションについて説明します。
C#でのアニメーションgifを表示する方法や、アニメーションの再生/停止方法についても紹介しますので、興味のある方はぜひ参考にしてみてはいかがでしょうか。
アニメーションgifの表示(PictureBox.Imageプロパティ)
C#では、PictureBox.Imageプロパティを使用して、アニメーションgifを表示できます。
実際のソースコードを見てみましょう。事前に、アニメーションgifを、””C:\test\test.gif””に格納しておいてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { PictureBox pictureBox; public Form1() { pictureBox = new PictureBox(); pictureBox.Location = new Point(10, 10); pictureBox.SizeMode = PictureBoxSizeMode.AutoSize; this.AutoSize = true; this.Load += new EventHandler(Form1_Load); this.Controls.Add(pictureBox); } private void Form1_Load(object sender, EventArgs e) { // アニメーションgifの設定 pictureBox.Image = Image.FromFile(@""C:\test\test.gif""); } } } |
実行すると、pictureBoxにアニメーションgifが表示されます。
このように、C#ではPictureBox.Imageプロパティを使用して、アニメーションgifを表示できます。
アニメーションgifの表示(ImageAnimatorクラス)Form編
C#では、ImageAnimatorクラスを使用して、Formにアニメーションgifを表示できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { Bitmap bitmap; public Form1() { this.AutoSize = true; this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { // アニメーションgifの設定 bitmap = new Bitmap(@""C:\test\test.gif""); // FormのPaintイベントハンドラ this.Paint += new PaintEventHandler(this.Form1_Paint); // アニメーション開始 ImageAnimator.Animate(bitmap, new EventHandler(this.Image_FrameChanged)); } private void Image_FrameChanged(object o, EventArgs e) { // PaintイベントハンドラをCall this.Invalidate(); } // FormのPaintイベントハンドラ private void Form1_Paint(object sender, PaintEventArgs e) { // フレームの更新 ImageAnimator.UpdateFrames(bitmap); // 画像の描画 e.Graphics.DrawImage(bitmap, 0, 0); } } } |
実行すると、Formにアニメーションgifが表示されます。
このように、C#ではImageAnimatorクラスを使用して、アニメーションgifを表示できます。
アニメーションgifの表示(ImageAnimatorクラス)PictureBox編
先ほどのサンプルコードはFormにアニメーションgifを表示しました。
C#では、ImageAnimatorクラスを使用して、PictureBoxにアニメーションgifを表示できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { Bitmap bitmap; PictureBox pictureBox; public Form1() { pictureBox = new PictureBox(); pictureBox.Location = new Point(10, 10); pictureBox.SizeMode = PictureBoxSizeMode.AutoSize; this.AutoSize = true; this.Load += new EventHandler(Form1_Load); this.Controls.Add(pictureBox); } private void Form1_Load(object sender, EventArgs e) { // アニメーションgifの設定 bitmap = new Bitmap(@""C:\test\test.gif""); pictureBox.Image = bitmap; // pictureBoxのPaintイベントハンドラ pictureBox.Paint += pictureBox_Paint; // アニメーション開始 ImageAnimator.Animate(bitmap, new EventHandler(Image_FrameChanged)); } private void Image_FrameChanged(object o, EventArgs e) { // Paintイベントハンドラを呼び出す pictureBox.Invalidate(); } // PictureBoxのPaintイベントハンドラ void pictureBox_Paint(object sender, PaintEventArgs e) { // フレームの更新 ImageAnimator.UpdateFrames(bitmap); // 画像の描画 e.Graphics.DrawImage(bitmap, 0, 0); } } } |
実行すると、PictureBoxにアニメーションgifが表示されます。
このように、C#ではImageAnimatorクラスを使用して、アニメーションgifを表示できます。
アニメーションgifの再生、停止
C#では、アニメーションgifの再生、停止ができます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { Button button1, button2; PictureBox pictureBox; public Form1() { // Startボタン button1 = new Button(); button1.Location = new Point(10, 10); button1.Text = ""Start""; button1.Enabled = false; button1.Click += Button1_Click; // Stopボタン button2 = new Button(); button2.Location = new Point(100, 10); button2.Text = ""Stop""; button2.Enabled = true; button2.Click += Button2_Click; pictureBox = new PictureBox(); pictureBox.Location = new Point(10, 40); pictureBox.SizeMode = PictureBoxSizeMode.AutoSize; this.AutoSize = true; this.Load += new EventHandler(Form1_Load); this.Controls.Add(pictureBox); this.Controls.Add(button1); this.Controls.Add(button2); } private void Form1_Load(object sender, EventArgs e) { // アニメーションgifの設定 pictureBox.Image = Image.FromFile(@""C:\test\test.gif""); } private void Button1_Click(object sender, EventArgs e) { // pictureBoxを有効にする(アニメーションが再生される) pictureBox.Enabled = true; button1.Enabled = false; button2.Enabled = true; } private void Button2_Click(object sender, EventArgs e) { // pictureBoxを無効にする(アニメーションが停止する) pictureBox.Enabled = false; button1.Enabled = true; button2.Enabled = false; } } } |
PictureBox.Enabledプロパティの有効/無効を切り替えることで、再生/停止ができます。
- SE
- プロパティの有効/無効の切り替えが必要だったんですね。よく分かりました。
- PM
- 参考になって何よりです。つまづいた部分を実際に書いてみてより理解を深めていってください。
C#を使ったアニメーションを正しく理解して使ってみましょう。
いかがでしたでしょうか。C#でアニメーションgifを表示する方法やアニメーションの停止方法について紹介しました。
ぜひ、ご自身でソースコードを書いて、理解を深めていきましょう。