drawimage动图,draw animals

http://www.itjxue.com  2023-01-07 08:18  来源:未知  点击次数: 

如何向winform窗体中插入动态图片

winfrom窗体中使用动态图:

方法一(简单):用label,然后设置为背景图片

方法二:使用GDI+?来实现?(很粗略的实现,没有帧间隔)

????????????Image?image?=?Image.FromFile("e://temp.gif");

?

????????????FrameDimension?fd?=?new?FrameDimension(image.FrameDimensionsList[0]);

????????????int?count?=?image.GetFrameCount(fd);

????????????Graphics?g?=?this.panel1.CreateGraphics();

????????????while?(true)

????????????{

?

????????????????for?(int?i?=?0;?i??count;?i++)

????????????????{

?

????????????????????//g.Clear(Color.White);

????????????????????image.SelectActiveFrame(fd,?i);

????????????????????g.DrawImage(image,?new?Point(0,?0));

????????????????????System.Threading.Thread.Sleep(100);

????????????????????Application.DoEvents();

????????????????}

????????????}

方法三:(推荐)

????????????Bitmap?animatedGif?=?new?Bitmap("e://temp2.gif");

????????????Graphics?g?=?this.panel1.CreateGraphics();

????????????//?A?Gif?image's?frame?delays?are?contained?in?a?byte?array

????????????//?in?the?image's?PropertyTagFrameDelay?Property?Item's

????????????//?value?property.

????????????//?Retrieve?the?byte?array...

????????????int?PropertyTagFrameDelay?=?0x5100;

????????????PropertyItem?propItem?=?animatedGif.GetPropertyItem(PropertyTagFrameDelay);

????????????byte[]?bytes?=?propItem.Value;

????????????//?Get?the?frame?count?for?the?Gif...

????????????FrameDimension?frameDimension?=?new?FrameDimension(animatedGif.FrameDimensionsList[0]);

????????????int?frameCount?=?animatedGif.GetFrameCount(FrameDimension.Time);

????????????//?Create?an?array?of?integers?to?contain?the?delays,

????????????//?in?hundredths?of?a?second,?between?each?frame?in?the?Gif?image.

????????????int[]?delays?=?new?int[frameCount?+?1];

????????????int?i?=?0;

????????????for?(i?=?0;?i?=?frameCount?-?1;?i++)

????????????{

????????????????delays[i]?=?BitConverter.ToInt32(bytes,?i?*?4);

????????????}

????????????//?Play?the?Gif?one?time...

????????????while?(true)

????????????{

????????????????for?(i?=?0;?i?=?animatedGif.GetFrameCount(frameDimension)?-?1;?i++)

????????????????{

????????????????????animatedGif.SelectActiveFrame(frameDimension,?i);

????????????????????g.DrawImage(animatedGif,?new?Point(0,?0));

????????????????????Application.DoEvents();

????????????????????Thread.Sleep(delays[i]?*?10);

????????????????}

????????????}

方法四:?使用.NET?自带的类:System.Drawing.ImageAnimator

最近在做一个图片查看器,由于使用一般的PctureBox,在性能和缩放控制上都无法满足预期的要求,因此所有组件的呈现均是通过重写控件的OnPaint事件来绘制。在查看gif图片时发现Graphics.DrawImage只呈现第一帧,无法满足预期要求,因此经过摸索寻找到了解决自绘gif的较好办法。

这里介绍一个.net自身携带的类ImageAnimator,这个类类似于控制动画的时间轴,使用ImageAnimator.CanAnimate可以判断一个图片是否为动画,调用ImageAnimator.Animate可以开始播放动画,即每经过一帧的时间触发一次OnFrameChanged委托,我们只要在该委托中将Image的活动帧选至下一帧再迫使界面重绘就可以实现动画效果了。

为了方便以后的使用,我将这些代码整合到了一起,形成一个AnimateImage类,该类提供了CanAnimate、FrameCount、CurrentFrame等属性,以及Play()、Stop()、Reset()等动画常用的方法,代码如下:

?

using?System;??????

using?System.Collections.Generic;??????

using?System.Text;??????

using?System.Drawing;??????

using?System.Drawing.Imaging;??????

?????

namespace?GifTest??????

{??????

????/**////?summary??????

??///?表示一类带动画功能的图像。??????

??///?/summary??????

??public?class?AnimateImage??????

???{??????

???????Image?image;??????

???????FrameDimension?frameDimension;??????

??????/**////?summary??????

??????///?动画当前帧发生改变时触发。??????

??????///?/summary??????

??????public?event?EventHandlerEventArgs?OnFrameChanged;??????

??

?????/**////?summary??????

??????///?实例化一个AnimateImage。??????

??????///?/summary??????

??????///?param?name="img"动画图片。/param??????

??????public?AnimateImage(Image?img)??????

???????{??????

???????????image?=?img;??????

??????????lock?(image)??????

??????????{??????

???????????????mCanAnimate?=?ImageAnimator.CanAnimate(image);??????

??????????????if?(mCanAnimate)??????

???????????????{??????

??????????????????Guid[]?guid?=?image.FrameDimensionsList;??????

???????????????????mFrameCount?=?image.GetFrameCount(frameDimension);??????

???????????????}??????

???????????}??????

???????}??????

??

?????bool?mCanAnimate;??????

?????int?mFrameCount?=?1,?mCurrentFrame?=?0;??????

??

??????/**////?summary??????

?????///?图片。??????

??????///?/summary??????

??????public?Image?Image??????

???????{??????

??????????get?{?return?image;?}??????

???????}??????

??

??????/**////?summary??????

??????///?是否动画。??????

?????///?/summary??????

??????public?bool?CanAnimate??????

???????{??????

??????????get?{?return?mCanAnimate;?}??????

??????}??????

??

???????/**////?summary??????

????///?总帧数。??????

??????///?/summary??????

??????public?int?FrameCount??????

???????{??????

???????????get?{?return?mFrameCount;?}??????

????????}??????

??

???????/**////?summary??????

???????///?播放的当前帧。??????

???????///?/summary??????

???????public?int?CurrentFrame??????

????????{??????

???????????get?{?return?mCurrentFrame;?}??????

????????}????????

???????/**////?summary??????

??????///?播放这个动画。??????

??????///?/summary??????

???????public?void?Play()??????

???????{??????

??????????if?(mCanAnimate)??????

???????????{??????

??????????????lock?(image)??????

???????????????{??????

???????????????????ImageAnimator.Animate(image,?new?EventHandler(FrameChanged));??????

???????????????}??????

???????????}??????

???????}??????

??

??????/**////?summary??????

??????///?停止播放。??????

??????///?/summary??????

??????public?void?Stop()??????

???????{??????

??????????if?(mCanAnimate)??????

???????????{??????

??????????????lock?(image)??????

???????????????{??????

???????????????????ImageAnimator.StopAnimate(image,?new?EventHandler(FrameChanged));??????

???????????????}??????

???????????}??????

???????}??????

??

??????/**////?summary??????

??????///?重置动画,使之停止在第0帧位置上。??????

??????///?/summary??????

??????public?void?Reset()??????

???????{??????

??????????if?(mCanAnimate)??????

???????????{??????

???????????????ImageAnimator.StopAnimate(image,?new?EventHandler(FrameChanged));??????

??????????????lock?(image)??????

???????????????{??????

???????????????????image.SelectActiveFrame(frameDimension,?0);??????

???????????????????mCurrentFrame?=?0;??????

???????????????}??????

???????????}??????

???????}??????

??

??????private?void?FrameChanged(object?sender,?EventArgs?e)??????

???????{??????

???????????mCurrentFrame?=?mCurrentFrame?+?1?=?mFrameCount???0?:?mCurrentFrame?+?1;??????

??????????lock?(image)??????

???????????{??????

???????????????image.SelectActiveFrame(frameDimension,?mCurrentFrame);??????

???????????}??????

??????????if?(OnFrameChanged?!=?null)??????

???????????{??????

???????????????OnFrameChanged(image,?e);??????

???????????}??????

???????}??????

???}??????

???

?

?

使用如下方法调用:

?

view?plaincopy?to?clipboardprint?

?

using?System;??????

using?System.Collections.Generic;??????

using?System.ComponentModel;??????

using?System.Data;??????

using?System.Drawing;??????

using?System.Drawing.Imaging;??????

using?System.Text;??????

using?System.Windows.Forms;??????

?????

namespace?GifTest??????

{??????

????public?partial?class?Form1?:?Form??????

?????{??????

?????????AnimateImage?image;??????

?????

????????public?Form1()??????

?????????{??????

?????????????InitializeComponent();??????

?????????????image?=?new?AnimateImage(Image.FromFile(@"C:/Documents?and?Settings/Administrator/My?Documents/My?Pictures/未命名.gif"));??????

?????????????image.OnFrameChanged?+=?new?EventHandlerEventArgs(image_OnFrameChanged);??????

?????????????SetStyle(ControlStyles.OptimizedDoubleBuffer?|?ControlStyles.AllPaintingInWmPaint?|?ControlStyles.UserPaint,?true);??????

?????????}??????

?????

????????void?image_OnFrameChanged(object?sender,?EventArgs?e)??????

?????????{??????

?????????????Invalidate();??????

?????????}??????

?????

????????private?void?Form1_Load(object?sender,?EventArgs?e)??????

?????????{??????

?????????????image.Play();??????

?????????}??????

?????

????????private?void?Form1_Paint(object?sender,?PaintEventArgs?e)??????

?????????{??????

????????????lock?(image.Image)??????

?????????????{??????

?????????????????e.Graphics.DrawImage(image.Image,?new?Point(0,?0));??????

?????????????}??????

?????????}??????

?????

????????private?void?button1_Click(object?sender,?EventArgs?e)??????

?????????{??????

????????????if?(button1.Text.Equals("Stop"))??????

?????????????{??????

?????????????????image.Stop();??????

?????????????????button1.Text?=?"Play";??????

?????????????}??????

????????????else?????

?????????????{??????

?????????????????image.Play();??????

?????????????????button1.Text?=?"Stop";??????

?????????????}??????

?????????????Invalidate();??????

?????????}??????

?????

????????private?void?button2_Click(object?sender,?EventArgs?e)??????

?????????{??????

?????????????image.Reset();??????

?????????????button1.Text?=?"Play";??????

?????????????Invalidate();??????

?????????}??????

?????}??????

}

html5 cavas 中用drawImage 加载gif不动

就是这样的 canvas里面用drawImage画gif只显示第一帧,不会动的。。。

canvas的drawimage方法没法绘制动态gif图

1.用img标签,然后浮在canvas上面,canvas和html结合作图才是王道。

2.读取gif的二进制数据,然后用一些算法解析出来,然后一桢一帧画出来,这个的确是有的,你可以在google搜一下,有一个框架,不过这种方法不太靠谱。就是玩的。

html5 canvas怎么让图片动起来, 我用的是drawImage画的 ,然后通过setInterval 定时的改变图像的位置.

刚好我这有点代码:

!doctype html

html

head

meta charset="utf-8"

!-- TemplateBeginEditable name="doctitle" --

title我的图片/title

/head

body

canvas id="Map" width="500" height="500" style="background:gray;"/canvas

script

// 设置绘图环境

var myMap = document.getElementById("Map");

var cxt = myMap.getContext('2d');

// 设置图像位置初始位置的变量

var x = 20;

var y = 20;

// 创建绘图对象,并且画出来

var img = new Image();

img.src = "img/u55.png";

draw();

function draw() {

cxt.clearRect(0, 0, 500, 500);

x += 1;

y += 5;

cxt.drawImage(img, x, y, 80, 80);

}

window.setInterval(draw, 100);

/script

/body

/html

你把图片的路径改为你的图片路径就行了!

(责任编辑:IT教学网)

更多