可一个图片显示GIF动画的Windows应用程序? [英] Can a PictureBox show animated GIF in Windows Application?

查看:354
本文介绍了可一个图片显示GIF动画的Windows应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想表现出一个gif动画在.net Winform的。 如何做到这一点?

我previously采用VB 6.0。非常感谢。

解决方案
  1. 将一个图片框在窗体上,然后指定一个扩展的Gif图片文件。或者:

  2. 编程方式动画GIF图像加载架与code一个图片,这里的的Gif类:

VB.NET

 公共类GifImage
    私人gifImage作为图像
    私人维度FrameDimension
    私人frameCount作为整数
    私人currentFrame为整数= -1
    专用反向作为布尔
    私人[步]作为整数= 1

    公共子新(路径作为字符串)
        gifImage = Image.FromFile(路径)
        '初始化
        维=新FrameDimension(gifImage.FrameDimensionsList(0))
        得到的GUID
            '在动画总帧数
        frameCount = gifImage.GetFrameCount(维)
    结束小组

    公共财产ReverseAtEnd()作为布尔
        的GIF是否应该发挥倒退,当它到达终点
        得到
            返回反转
        最终获取
        组
            反向=价值
        结束设定
    高端物业

    公共功能GetNextFrame()作为图像

        currentFrame + = [步骤]

        如果动画达到的边界?
        如果currentFrame> = frameCount OrElse运算currentFrame< 1然后
            如果反向然后
                [步骤] * = -1
                ......扭转计
                    应用它
                currentFrame + = [步骤]
            其他
                currentFrame = 0
                ...或重新开始
            结束如果
        结束如果
        返回的getFrame(currentFrame)
    端功能

    公共功能的getFrame(指数为整数)作为图像
        gifImage.SelectActiveFrame(尺寸,索引)
        寻找帧
        返回DirectCast(gifImage.Clone(),图像)
        返回它的一个副本
    端功能
末级
 

C#

 公共类GifImage
{
    私人形象gifImage;
    私人FrameDimension尺寸;
    私人诠释frameCount;
    私人诠释currentFrame = -1;
    私人BOOL反转;
    私人诠释步= 1;

    公共GifImage(字符串路径)
    {
        gifImage = Image.FromFile(路径);
        //初始化
        维=新FrameDimension(gifImage.FrameDimensionsList [0]);
        //获取GUID
        //在动画总帧数
        frameCount = gifImage.GetFrameCount(尺寸);
    }

    公共BOOL ReverseAtEnd {
        //该GIF是否应该发挥倒退,当它到达终点
        {返回相反; }
        集合{反转=价值; }
    }

    公众形象GetNextFrame()
    {

        currentFrame + =步骤;

        //如果动画达到的边界?
        如果(currentFrame&GT = frameCount || currentFrame&小于1){
            如果(反向){
                步骤* = -1;
                //...reverse计数
                //应用它
                currentFrame + =步骤;
            }
            其他 {
                currentFrame = 0;
                //...or重新开始
            }
        }
        返回的getFrame(currentFrame);
    }

    公众形象的getFrame(INT指数)
    {
        gifImage.SelectActiveFrame(尺寸,索引);
        //寻找帧
        回报(图)gifImage.Clone();
        //返回它的一个副本
    }
}
 

C#用法:

打开一个WinForm项目拖放在一个图片,一个定时器和一个按钮,上面显示的 GifImage.cs 类。

 公共部分类Form1中:形态
{
    私人GifImage gifImage = NULL;
    私人字符串文件路径= @C:\用户\杰里米\桌面\ ExampleAnimation.gif;

    公共Form1中()
    {
        的InitializeComponent();
        // a)正常方式
        //pictureBox1.Image = Image.FromFile(文件路径);

        // b)我们控制动画
        gifImage =新GifImage(文件路径);
        gifImage.ReverseAtEnd = FALSE; //不扭转,在结束
    }

    私人无效的button1_Click(对象发件人,EventArgs的)
    {
        //开始时间/动画
        timer1.Enabled = TRUE;
    }

    //这是动画帧事件
    私人无效timer1_Tick(对象发件人,EventArgs的)
    {
        pictureBox1.Image = gifImage.GetNextFrame();
    }
}
 

I would like to show a animated gif in .Net Winform. How to do this?

I previously used VB 6.0. Thanks very much.

解决方案

  1. Put a picturebox on a form and then specify a picture file with a Gif extension. Or:

  2. Programatically animate a gif Image loading frames into a PictureBox with code, here's the Gif class:

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# usage:

Open a Winform project a drag and drop in a PictureBox, a Timer and a Button, with the GifImage.cs class shown above.

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();
    }
}

这篇关于可一个图片显示GIF动画的Windows应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆