2012-11-21 18 views

risposta

16
  1. Mettere un PictureBox in un modulo e quindi specificare un file di immagine con estensione GIF. Oppure:

  2. Programatically animare un frame gif caricamento delle immagini in un PictureBox con il codice, ecco la classe 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# utilizzo:

Aprire un progetto Winform un drag and drop in un PictureBox, un timer e un pulsante, con la classe GifImage.cs mostrato sopra.

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

Puoi dirmi come utilizzare lo snippet? Voglio dire come caricare un'immagine in una picturebox usando quella classe, non riesco a trovare il modo di usare quel codice, ma è interessante ... – ElektroStudios

+0

Qual è il problema? –

+0

Ad esempio: Dim asdasd As New GifImage = "C: \ image.gif " PictureBox1.Image = GifImage (" C: \ image.gif ") il problema è che non so come usarlo, scusa sono un novellino, se mi puoi spiegare per favore ... I voglio caricarlo manualmente – ElektroStudios

9

Sviluppare su @ risposta di JeremyThompson Vorrei aggiungere un frammento di codice per mostrare come è possibile implementare il primo approccio, perché è molto più semplice, e non richiede di animare manualmente la gif, visto che il PictureBox ha una funzione integrata per gestire uno scenario del genere. Basta aggiungere un PictureBox al form, e nel costruttore modulo assegnare il percorso dell'immagine al PictureBox.ImageLocation

C#

public PictureForm() 
{ 
     InitializeComponent(); 
     pictureBoxGif.ImageLocation = "C:\\throbber.gif"; 
} 

VB.Net

Public Sub New() 
    InitializeComponent() 
    pictureBoxGif.ImageLocation = "C:\throbber.gif" 
End Sub 

a mio parere questo è un molto più soluzione più semplice, soprattutto per chi è nuovo a .NET.

+2

+1 per sportività, ** se solo le cose fossero così facili in VB6 **. La 'gioia di .Net', in realtà, mentre continuo a crescere, noto quanto sia meglio un framework gestito !!! –

+0

+1 per dare rilevanza alla tua soluzione, semplice e perfetto Si noti che la disattivazione della casella di immagine (o del modulo) bloccherà l'animazione della gif! – Marco

1

Ho giocato con questo e le riproduzioni di animazione a condizione che non si esegua un'altra operazione di lunga durata sullo stesso thread. Nel momento in cui si esegue un'altra operazione di lunga durata, si vorrebbe farlo in un altro thread.

Il modo più semplice per eseguire questa operazione consiste nell'utilizzare il componente BackgroundWorker che è possibile trascinare sul modulo dalla casella degli strumenti. Inseriresti quindi il codice dell'operazione a esecuzione prolungata nell'evento DoWork() di BackgroundWorker. Il passaggio finale consiste nel richiamare il codice chiamando il metodo RunWorkerAsync() dell'istanza di BackgroundWorker.

Problemi correlati