GIF animate

giovedì 03 settembre 2009 - 00.54

tizio90 Profilo | Newbie

Salve,
ho bisogno di visualizzare gif animate nel form della mia applicazione, ma non riesco ad animarle correttamente utilizzando la classe ImageAnimator: alcune frames dell'animazione non vengono visualizzate (letteralmente "saltano"). Qualcuno può aiutarmi? Grazie in anticipo per le risposte

Bitmap banimage; EventHandler eh; [...] private void start_animation() { banimage = new Bitmap("banimage.gif"); eh = new EventHandler(OnFrameChanged); ImageAnimator.Animate(banimage, eh); } private void OnFrameChanged(object o, EventArgs e) { ImageAnimator.UpdateFrames(); pictureBox4.Invalidate(); } private void pictureBox4_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawImage(banimage, 0, 0); }

freeteo Profilo | Guru

Ciao,
a mio avviso il problema è che forse il tuo applicativo è impegnato a far qualcsosa e non viene "refreshato" il controllo pictureBox per fare quindi il Repaint.
Avevo usato anche io l'oggetto GifAnimator, ma per problemi simili ai tuoi avevo fatto un componentino veloce ("PictureBoxAnimated" che eredita da PictureBox) che tramite timer mi andasse a fare il refresh dell'immagine, il codice è questo:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Drawing.Imaging; namespace Freeteo.Web.ContentManagement.Controlli { public partial class PictureBoxAnimated : PictureBox { System.Timers.Timer timeRefresh = new System.Timers.Timer(100); List<Image> pagine = new List<Image>(); Image immagine; public new Image Image { get { return immagine; } set { immagine = value; FrameDimension frameDim = new FrameDimension(immagine.FrameDimensionsList[0]); int frameCount = immagine.GetFrameCount(frameDim); //--- se ha piu' immagini dentro (quindi gif animata) if (frameCount > 1) { for (int i = 0; i < frameCount; i++) { immagine.SelectActiveFrame(frameDim, i); pagine.Add(immagine.Clone() as Image); } timeRefresh.Enabled = true; } else base.Image = immagine; } } public PictureBoxAnimated() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; timeRefresh.Elapsed += new System.Timers.ElapsedEventHandler(timeRefresh_Elapsed); } int i = 0; void timeRefresh_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //--- se sono in design di visual studio if (DesignMode && base.Image == null) { base.Image = immagine; return; } if (Visible) { using (Graphics graph = this.CreateGraphics()) { graph.Clear(this.BackColor); graph.DrawImage(pagine[i++ % pagine.Count], 0, 0, this.Width, this.Height); } } } } }


Sinceramente non è che sia proprio elegante come flusso di codice, ma l'ho usata nelle mie form senza problemi, vedi tu se può andare bene anche per te (ps: dopo che compili una volta lo trovi nella Toolbox e quindi riesci a fare Drag&Drop)

Ciao.

Matteo Raumer
[MVP Visual C#]
http://blogs.dotnethell.it/freeteo
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5