C#でのアニメーション再生・停止方法についてご紹介!
- システム
エンジニア - C#でのアニメーションがうまく動かせなくて困っています。
- プロジェクト
マネージャー - なるほど。ではアニメーション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プロパティの有効/無効を切り替えることで、再生/停止ができます。
- システム
エンジニア - プロパティの有効/無効の切り替えが必要だったんですね。よく分かりました。
- プロジェクト
マネージャー - 参考になって何よりです。つまづいた部分を実際に書いてみてより理解を深めていってください。
C#を使ったアニメーションを正しく理解して使ってみましょう。
いかがでしたでしょうか。C#でアニメーションgifを表示する方法やアニメーションの停止方法について紹介しました。
ぜひ、ご自身でソースコードを書いて、理解を深めていきましょう。
FEnet.NETナビ・.NETコラムは株式会社オープンアップシステムが運営しています。

株式会社オープンアップシステムはこんな会社です
秋葉原オフィスには株式会社オープンアップシステムをはじめグループのIT企業が集結!
数多くのエンジニアが集まります。

-
スマホアプリから業務系システムまで

スマホアプリから業務系システムまで開発案件多数。システムエンジニア・プログラマーとしての多彩なキャリアパスがあります。
-
充実した研修制度

毎年、IT技術のトレンドや社員の要望に合わせて、カリキュラムを刷新し展開しています。社内講師の丁寧なサポートを受けながら、自分のペースで学ぶことができます。
-
資格取得を応援

スキルアップしたい社員を応援するために資格取得一時金制度を設けています。受験料(実費)と合わせて資格レベルに合わせた最大10万円の一時金も支給しています。
-
東証プライム上場企業グループ

オープンアップシステムは東証プライム上場「株式会社夢真ビーネックスグループ」のグループ企業です。
安定した経営基盤とグループ間のスムーズな連携でコロナ禍でも安定した雇用を実現させています。
株式会社オープンアップシステムに興味を持った方へ
株式会社オープンアップシステムでは、開発系エンジニア・プログラマを募集しています。
年収をアップしたい!スキルアップしたい!大手の上流案件にチャレンジしたい!
まずは話だけでも聞いてみたい場合もOK。お気軽にご登録ください。

C#新着案件New Job
-
システム開発/東京都新宿区/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円東京都新宿区(新宿駅) -
システム開発/東京都新宿区/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円東京都新宿区(新宿駅) -
デバック、テスト項目の作成/神奈川県横浜市/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円神奈川県横浜市(桜木町駅) -
デバック、テスト項目の作成/神奈川県横浜市/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円神奈川県横浜市(桜木町駅) -
基幹システム開発導入/東京都新宿区/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円東京都新宿区(西新宿駅) -
基幹システム開発導入/東京都新宿区/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円東京都新宿区(西新宿駅)





