2012-11-21 202 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()方法來調用您的代碼。