2012-11-21 51 views

回答

16
  1. 把一个图片框放在一个表格上,然后用一个Gif扩展名指定一个图片文件。或者:

  2. 编程方式动画GIF图像加载架与代码中的图片框,这里的的Gif类:

VB.NET

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 < 1 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#

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 < 1) { 
      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,一个定时器和一个按钮拖放,与上文所示的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(); 
    } 
} 

enter image description here

+0

你能告诉我如何使用代码片段吗?我的意思是如何在使用该类的图片框中加载图片,我无法找到使用该代码的方式,但很有趣... – ElektroStudios

+0

最新问题? –

+0

例如:Dim asdasd As New GifImage =“C:\ image。gif“ PictureBox1.Image = GifImage(”C:\ image.gif“)问题是我不知道如何使用它,抱歉,我是一个小新手,如果你能解释我请...我想要手动加载 – ElektroStudios

9

开发上@ JeremyThompson的答案,我想添加一段代码来向您展示如何实现第一种方法,因为它是简单了很多,而且不需要你手动动画该gif,看到PictureBox有一个内置的功能来处理这种情况。 只需添加一个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 

在我oppinion这是一个非常更简单的解决方案,特别适用于.NET新手。

+2

+1为体育道德, **如果在VB6中只有这些东西很简单**。“.Net'的喜悦,真的,随着我继续增长,我注意到托管框架的好处多多! –

+0

+1与您的解决方案相关,简单和完美请注意,禁用图片框(或表单)将阻止gif的动画! – Marco

1

我已经玩过这个和动画剧,只要你不在同一个线程上执行另一个长时间运行的操作。当你执行另一个长时间运行的操作时,你会想在另一个线程中执行它。

执行此操作的最简单方法是使用可从工具箱拖到表单上的BackgroundWorker组件。然后,您将长时间运行的操作代码放入BackgroundWorker的DoWork()事件中。最后一步是通过调用BackgroundWorker实例的RunWorkerAsync()方法来调用您的代码。