.Net Winform でアニメーション gif を表示したいと思います。これを行う方法?
以前はVB6.0を使用していました。
.Net Winform でアニメーション gif を表示したいと思います。これを行う方法?
以前はVB6.0を使用していました。
PictureBox
フォームに を配置し、拡張子が Gif の画像ファイルを指定します。または:
フレームをコードにロードする gif 画像をプログラムでアニメーション化PictureBox
します。Gif クラスは次のとおりです。
VB.NET
Imports System.Drawing.Imaging
Imports System.Drawing
Public Class GifImage
Private gifImage As Image
Private dimension As FrameDimension
Private frameCount As Integer
Private currentFrame As Integer = -1
Private reverse As Boolean
Private [step] As Integer = 1
Public Sub New(path As String)
gifImage = Image.FromFile(path)
'initialize
dimension = New FrameDimension(gifImage.FrameDimensionsList(0))
'gets the GUID
'total frames in the animation
frameCount = gifImage.GetFrameCount(dimension)
End Sub
Public Property ReverseAtEnd() As Boolean
'whether the gif should play backwards when it reaches the end
Get
Return reverse
End Get
Set
reverse = value
End Set
End Property
Public Function GetNextFrame() As Image
currentFrame += [step]
'if the animation reaches a boundary...
If currentFrame >= frameCount OrElse currentFrame < 0 Then
If reverse Then
[step] *= -1
'...reverse the count
'apply it
currentFrame += [step]
Else
currentFrame = 0
'...or start over
End If
End If
Return GetFrame(currentFrame)
End Function
Public Function GetFrame(index As Integer) As Image
gifImage.SelectActiveFrame(dimension, index)
'find the frame
Return DirectCast(gifImage.Clone(), Image)
'return a copy of it
End Function
End Class
C#
using System.Drawing.Imaging;
using System.Drawing;
public class GifImage
{
private Image gifImage;
private FrameDimension dimension;
private int frameCount;
private int currentFrame = -1;
private bool reverse;
private int step = 1;
public GifImage(string path)
{
gifImage = Image.FromFile(path);
//initialize
dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
//gets the GUID
//total frames in the animation
frameCount = gifImage.GetFrameCount(dimension);
}
public bool ReverseAtEnd {
//whether the gif should play backwards when it reaches the end
get { return reverse; }
set { reverse = value; }
}
public Image GetNextFrame()
{
currentFrame += step;
//if the animation reaches a boundary...
if (currentFrame >= frameCount || currentFrame < 0) {
if (reverse) {
step *= -1;
//...reverse the count
//apply it
currentFrame += step;
}
else {
currentFrame = 0;
//...or start over
}
}
return GetFrame(currentFrame);
}
public Image GetFrame(int index)
{
gifImage.SelectActiveFrame(dimension, index);
//find the frame
return (Image)gifImage.Clone();
//return a copy of it
}
}
C# の使用法:
上に示したクラスを使用して、Winform プロジェクトをドラッグ アンド ドロップしてPictureBox
、Timer と Button を開きます。GifImage.cs
public partial class Form1: Form
{
private GifImage gifImage = null;
private string filePath = @"C:\Users\Jeremy\Desktop\ExampleAnimation.gif";
public Form1()
{
InitializeComponent();
//a) Normal way
//pictureBox1.Image = Image.FromFile(filePath);
//b) We control the animation
gifImage = new GifImage(filePath);
gifImage.ReverseAtEnd = false; //dont reverse at end
}
private void button1_Click(object sender, EventArgs e)
{
//Start the time/animation
timer1.Enabled = true;
}
//The event that is animating the Frames
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = gifImage.GetNextFrame();
}
}
@JeremyThompsonの回答に基づいて開発するコードスニペットを追加して、最初のアプローチを実装する方法を示したいと思います。これは、はるかに単純であり、gifPictureBox
が組み込まれていることを確認して、手動でgifをアニメーション化する必要がないためです。そのようなシナリオを処理します。フォームにを追加するだけPictureBox
で、フォームコンストラクターで画像パスをに割り当てます。PictureBox.ImageLocation
C#
public PictureForm()
{
InitializeComponent();
pictureBoxGif.ImageLocation = "C:\\throbber.gif";
}
VB.Net
Public Sub New()
InitializeComponent()
pictureBoxGif.ImageLocation = "C:\throbber.gif"
End Sub
私の意見では、これは、特に.NETを初めて使用する人にとっては、はるかに単純なソリューションです。
私はこれをいじってみましたが、同じスレッドで別の長時間実行操作を実行しない限り、アニメーションが再生されます。別の長時間実行される操作を実行する瞬間、別のスレッドで実行したいと思うでしょう。
これを行う最も簡単な方法は、ツールボックスからフォームにドラッグできる BackgroundWorker コンポーネントを使用することです。次に、実行時間の長い操作コードを BackgroundWorker の DoWork() イベントに配置します。最後の手順は、BackgroundWorker インスタンスの RunWorkerAsync() メソッドを呼び出してコードを呼び出すことです。