您的位置:首页 > 编程语言 > C#

Windows Mobile C#实现GIF图片播放

2013-06-19 10:13 176 查看
网上找个很久关于Mobile6.5播放动态gif的资料,大部分资料都是以下废话。。。

1. 使用WebBrowser控件

2. 使用微软为Wince补充的AnimationControl控件

3. 使用网络上流传的ImageCtrl.dll

4. 仿照Win32代码用C#解码GIF播放

发现1不好,因为还得请求一次链接。

webBrowser.Url =new System.Uri("gif文件");

webBrowser是工具箱中的控件,拖过来就能用。

ImageCtrl.dll 大部分都是C++写的,对于我这种入门级C#菜鸟而言,引用并不好,改写为C#版本后,发现能支持特定gif,自己做的gif有部分不行,不知道啥原因。

最后硬着头皮用微软的AnimationControl控件,代码如下:

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Imaging;

namespace AnimateControl

{

public enum FrameLayouts

{

Horisontal = 0,

Vertical = 1

}

/// <summary>

/// Summary description for AnimateCtl.

/// </summary>

public class AnimateCtl : System.Windows.Forms.Control

{

private Bitmap bitmap;

private int frameCount;

private int frameWidth;

private int frameHeight;

private Graphics graphics;

private int currentFrame = 0;

private int loopCount = 0;

private int loopCounter = 0;

private DateTimePicker dateTimePicker1;

private System.Windows.Forms.Timer fTimer;

public Bitmap Bitmap

{

get { return bitmap; }

set

{

bitmap = value;

}

}

public AnimateCtl()

{

//Cache the Graphics object

graphics = this.CreateGraphics();

//Instanciate the Timer

fTimer = new System.Windows.Forms.Timer();

//Hook up to the Timer's Tick event

fTimer.Tick += new System.EventHandler(this.timer1_Tick);

}

private void timer1_Tick(object sender, System.EventArgs e)

{

if (loopCount == -1) //loop continuosly

{

this.DrawFrame();

}

else

{

if (loopCount == loopCounter) //stop the animation

fTimer.Enabled = false;

else

this.DrawFrame();

}

}

public void StartAnimation(int frWidth, int DelayInterval, int LoopCount)

{

frameWidth = frWidth;

//How many times to loop

loopCount = LoopCount;

//Reset loop counter

loopCounter = 0;

//Calculate the frameCount

frameCount = bitmap.Width / frameWidth;

frameHeight = bitmap.Height;

//Resize the control

this.Size = new Size(frameWidth, frameHeight);

//Assign delay interval to the timer

fTimer.Interval = DelayInterval;

//Start the timer

fTimer.Enabled = true;

}

public void StopAnimation()

{

fTimer.Enabled = false;

}

private void DrawFrame()

{

if (currentFrame < frameCount - 1)

{

currentFrame++;

}

else

{

//increment the loopCounter

loopCounter++;

currentFrame = 0;

}

Draw(currentFrame);

}

private void Draw(int iframe)

{

//Calculate the left location of the drawing frame

int XLocation = iframe * frameWidth;

Rectangle rect = new Rectangle(XLocation, 0, frameWidth, frameHeight);

//Draw image

graphics.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);

}

private void InitializeComponent()

{

this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();

this.SuspendLayout();

//

// dateTimePicker1

//

this.dateTimePicker1.Location = new System.Drawing.Point(0, 0);

this.dateTimePicker1.Name = "dateTimePicker1";

this.dateTimePicker1.Size = new System.Drawing.Size(200, 22);

this.dateTimePicker1.TabIndex = 0;

this.ResumeLayout(false);

}

}

}

说白了,就是把gif动态图做成静态的一帧一帧的,然后去调用

animCtl = new AnimateCtl();

//Assign the image file

animCtl.Bitmap = BarCodeDemo.Properties.Resources.dengdai2;

//Set the location

animCtl.Location = new Point(0, 0);

//Add to control to the Form

关于gif变成一帧一帧的工具,比如Gif分离器

this.panel1.Controls.Add(animCtl);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: