zoukankan      html  css  js  c++  java
  • ASP.NET GDI+生成动态的Gif动画.


    update : DEOM download from here


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

    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;


    还有一篇介绍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();
  • 相关阅读:
    Java速成笔记
    C语言学习笔记
    形式语义05 Semantics
    密码学04 PRG&PRF
    形式语义04 Types
    密码学03 Computational
    形式语义03 Lambda
    密码学02 Perfect
    形式语义01 Intro
    密码学01 Intro
  • 原文地址:https://www.cnblogs.com/caca/p/228687.html
Copyright © 2011-2022 走看看