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 * 4, 20);
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(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();
GifImage.GifAnimation gif = new GifImage.GifAnimation(bmp,
GifImage.GraphicControlExt.Default);
gif.Application = GifImage.ApplicationExt.Default;
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 * 4, 20);
g.DrawString("Yeah", f, Brushes.Orange, 60, -20+i*4);
g.Flush();
gif.AddFrame(bmp);
}
f.Dispose();
g.Dispose();
bmp.Dispose();
FileStream fs = new FileStream(@"E:\vmlinux\GifImage.gif", FileMode.Create);
gif.Save(fs);
fs.Close;
Load and modify or extract frames from existing Gif animation
FileInfo fi = new FileInfo(@"E:\vmlinux\x.gif");
GifImage.GifAnimation gif = new GifImage.GifAnimation(fi.OpenRead());
gif.Control = new GifImage.GraphicControlExt(
1,
false,
false,
36,
0);
gif.AddFrame(new Bitmap(@"E:\vmlinux\2.bmp"));
gif.InsertFrame(new Bitmap(@"E:\vmlinux\1.bmp"),2);
FileInfo frm = new FileInfo(@"E:\vmlinux\frm.gif");
gif.SaveFrame(frm.OpenWrite(),4);
FileStream fs = new FileStream(@"E:\vmlinux\out.gif", FileMode.Create);
gif.Save(fs);
fs.Close();
posted @ 2005-09-02 12:57
卡卡 ^ cacard 阅读(2607)
评论(4) 编辑 收藏 所属分类:
ASP.NET