文章目錄
- Draw frames and pack into an animation
- Load and modify or extract frames from existing Gif animation
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 componentDraw 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();//(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;
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();