代码之家  ›  专栏  ›  技术社区  ›  Stuart Helwig

如何在Windows窗体(c#)上显示动画GIF

  •  132
  • Stuart Helwig  · 技术社区  · 18 年前

    我想显示一个动画GIF,使过程具有某种活动的感觉(例如,文件从一台计算机飞到另一台计算机,如Windows复制过程)。

    6 回复  |  直到 18 年前
        1
  •  262
  •   ΩmegaMan    6 年前

    这不太难。

    1. 在你的表格上放一个图片盒。
    2. 加载时显示picturebox。

    • 禁用picturebox将阻止gif设置动画。

    动画GIF:

    如果您正在寻找动画GIF,您可以生成它们:

    AjaxLoad - Ajax Loading gif generator

    另一种方法是:

    code project

        2
  •  12
  •   Aruch    15 年前

    我也有同样的问题。整个表单(包括gif)由于在后台长时间运行而停止重新绘制自身。下面是我如何解决这个问题的。

      private void MyThreadRoutine()
      {
       this.Invoke(this.ShowProgressGifDelegate);
       //your long running process
       System.Threading.Thread.Sleep(5000);
       this.Invoke(this.HideProgressGifDelegate);
      }
    
      private void button1_Click(object sender, EventArgs e)
      {
       ThreadStart myThreadStart = new ThreadStart(MyThreadRoutine);
       Thread myThread = new Thread(myThreadStart);
       myThread.Start(); 
      }
    

    我只是创建了另一个线程来负责这个操作。多亏了这个初始表单,我可以毫无问题地继续重新绘制(包括我的gif工作)。ShowProgressGifDelegate和HideProgressGifDelegate是将带有gif的pictureBox的visible属性设置为true/false的表单委托。

        3
  •  7
  •   PhiLho    18 年前

    请注意,在Windows中,传统上不使用动画GIF,而是使用少量AVI动画:有一个Windows本机控件仅用于显示它们。甚至有工具可以将动画GIF转换为AVI(反之亦然)。

        4
  •  3
  •   Grank    18 年前

    如果你把它放在PictureBox控件中,它应该可以正常工作

        5
  •  1
  •   marc marc    17 年前

        6
  •  1
  •   Gehan Fernando    6 年前
    Public Class Form1
    
        Private animatedimage As New Bitmap("C:\MyData\Search.gif")
        Private currentlyanimating As Boolean = False
    
        Private Sub OnFrameChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
            Me.Invalidate()
    
        End Sub
    
        Private Sub AnimateImage()
    
            If currentlyanimating = True Then
                ImageAnimator.Animate(animatedimage, AddressOf Me.OnFrameChanged)
                currentlyanimating = False
            End If
    
        End Sub
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    
            AnimateImage()
            ImageAnimator.UpdateFrames(animatedimage)
            e.Graphics.DrawImage(animatedimage, New Point((Me.Width / 4) + 40, (Me.Height / 4) + 40))
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            BtnStop.Enabled = False
    
        End Sub
    
        Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStop.Click
    
            currentlyanimating = False
            ImageAnimator.StopAnimate(animatedimage, AddressOf Me.OnFrameChanged)
            BtnStart.Enabled = True
            BtnStop.Enabled = False
    
        End Sub
    
        Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
    
            currentlyanimating = True
            AnimateImage()
            BtnStart.Enabled = False
            BtnStop.Enabled = True
    
        End Sub
    
    End Class
    
        7
  •  0
  •   M. Fawad Surosh    9 年前

    我遇到了同样的问题,通过实施我曾经面临的几个不同问题,我遇到了不同的解决方案。最后,下面是我将不同帖子中的一些片段放在一起的内容,这些内容对我来说正如预期的那样有效。

    private void btnCompare_Click(object sender, EventArgs e)
    {
        ThreadStart threadStart = new ThreadStart(Execution);
        Thread thread = new Thread(threadStart);
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }
    

    private void Execution()
    {
        btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = true; });
        Application.DoEvents();
    
        // Your main code comes here . . .
    
        btnCompare.Invoke((MethodInvoker)delegate { pictureBox1.Visible = false; });
    }
    

    请记住,PictureBox在“属性”窗口中不可见,或者执行以下操作:

    private void ComparerForm_Load(object sender, EventArgs e)
    {
        pictureBox1.Visible = false;
    }