ASP.NET GDI+產生動態Gif動畫.

來源:互聯網
上載者:User
文章目錄
  • 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();
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.