ASP.NET GDI+生成动态的Gif动画.


update : DEOM download from here


想用GDI+生成动画式的Gif图片,Google一把,还真的找到了,在CodeProject上面的一篇文章Create Gif animation on fly,作者用了一个组件,可惜组件的源代码没有放出来,Demo在这里下载,示例代码:

Bitmap bmp  =   new  Bitmap( 100 100 );
Graphics g 
=  Graphics.FromImage(bmp);
Font f 
=   new  Font( " arial " , 11f);
Brush b 
=  Brushes.Blue;

string  txt  =   " Rotate text animation! " ;
SizeF sz 
=  g.MeasureString(txt, f);
g.Clear(Color.WhiteSmoke);
g.DrawString(txt, f, b, 
50 - sz.Width / 2 50 - sz.Height / 2 );
g.Flush();
// (The following code create a starting frame from bmp)
GifImage.GifAnimation gif  =   new  GifImage.GifAnimation(bmp, 
    GifImage.GraphicControlExt.Default);
// (Set this property otherwise the animation will not play circularly)
gif.Application  =  GifImage.ApplicationExt.Default;
// (Use global color table only, set this option will greatly decrease the size of output file)
gif.UseGlobalColorTableOnly  =   true ;

for  ( int  i  =   1 ; i  <   36 ++ i)
{
    g.Clear(Color.WhiteSmoke);
    g.TranslateTransform(
50,50);
    g.RotateTransform(10f 
* i);
    g.DrawString(txt, f, b, sz.Width
/-2, sz.Height/-2);
    g.ResetTransform();
    g.DrawString(
"Hello", f, Brushes.Red, -50 + i * 420);
    g.DrawString(
"Yeah", f, Brushes.Orange, 60-20+i*4);

    g.Flush();
//(Create a frame from bitmap)
    gif.AddFrame(bmp);
}


f.Dispose();
g.Dispose();
bmp.Dispose();

FileStream fs 
=   new  FileStream( @" E:\vmlinux\GifImage.gif " , FileMode.Create);
// (Write animation to GifImage.gif)
gif.Save(fs);
fs.Close;


还有一篇介绍Gif文件结构的文章:
http://www.hf110.com/Article/jswz/bcjs/ozh/200504/889.html

参考原文:

Introduction

.NET Framework does not provide a way to create gif animations. Finally we have to do it ourselves. This is my solution, I write it down and share my component with everyone who loves Codeproject. Source code is currently not shared.

Because of server error , demo and picture upload is failed. Here is another download address http://www.newsmth.net/bbscon.php?ftype=0&bid=288&id=50708&ap=2454
Sorry for inconvenience, here is an updated demo package with test images http://www.newsmth.net/bbscon.php?ftype=0&bid=288&id=50723&ap=495
It's pain to upload files onto codeproject server :)

Use this component

Draw frames and pack into an animation

The following code piece will create GifImage.gif

Bitmap bmp = new Bitmap(100100);
Graphics g = Graphics.FromImage(bmp);
Font f = new Font("arial", 11f);
Brush b = Brushes.Blue;

string txt = "Rotate text animation!";
SizeF sz = g.MeasureString(txt, f);
g.Clear(Color.WhiteSmoke);
g.DrawString(txt, f, b, 50-sz.Width/250-sz.Height/2);
g.Flush();
//(The following code create a starting frame from bmp)
GifImage.GifAnimation gif = new GifImage.GifAnimation(bmp, 
    GifImage.GraphicControlExt.Default);
//(Set this property otherwise the animation will not play circularly)
gif.Application = GifImage.ApplicationExt.Default;
//(Use global color table only, set this option will greatly decrease the size of output file)
gif.UseGlobalColorTableOnly = true;

for (int i = 1; i < 36; ++i)
{
    g.Clear(Color.WhiteSmoke);
    g.TranslateTransform(50,50);
    g.RotateTransform(10f * i);
    g.DrawString(txt, f, b, sz.Width/-2, sz.Height/-2);
    g.ResetTransform();
    g.DrawString("Hello", f, Brushes.Red, -50 + i * 420);
    g.DrawString("Yeah", f, Brushes.Orange, 60, -20+i*4);

    g.Flush();
//(Create a frame from bitmap)
    gif.AddFrame(bmp);
}

f.Dispose();
g.Dispose();
bmp.Dispose();

FileStream fs = new FileStream(@"E:\vmlinux\GifImage.gif", FileMode.Create);
//(Write animation to GifImage.gif)
gif.Save(fs);
fs.Close;

Load and modify or extract frames from existing Gif animation

FileInfo fi = new FileInfo(@"E:\vmlinux\x.gif");
//(Load animation from file)
GifImage.GifAnimation gif = new GifImage.GifAnimation(fi.OpenRead());
//(set global animation control)
gif.Control = new GifImage.GraphicControlExt(
  1/*(Disposal Method)*/, 
  false/*(wait for user input and then continue?)*/, 
  false/*(set this option if transparent color is used)*/, 
  36/*(time to wait before the next frame is displayed)*/, 
  0/*(index of transparent color in color table)*/);
//(add a new frame to the end)
gif.AddFrame(new Bitmap(@"E:\vmlinux\2.bmp"));
//(insert a new frame at the specified position)
gif.InsertFrame(new Bitmap(@"E:\vmlinux\1.bmp"),2);

FileInfo frm = new FileInfo(@"E:\vmlinux\frm.gif");
//(save the frame at the specified position)
gif.SaveFrame(frm.OpenWrite(),4);

FileStream fs = new FileStream(@"E:\vmlinux\out.gif", FileMode.Create);
//(write the modified animation to file)
gif.Save(fs);
fs.Close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值