精灵和C#动画

时间:2009-12-03 14:28:14

标签: c# animation sprite

我发现了一个类来制作一个包含多个帧动画的gif文件,它在背景图像前面运行。这是我的班级:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace AnimSprites {

     public class AnimSprite {
     private int frame, interval, width, height;
     private string imgFile;
     private Image img;
     private Timer frameTimer;

     public AnimSprite(string f_imgFile, int f_width) {
          frame = 0;
          width = f_width;
          imgFile = f_imgFile;

          img = new Bitmap(imgFile);
          height = img.Height;
     }

     public void Start(int f_interval) {
          interval = f_interval;

          frameTimer = new Timer();
          frameTimer.Interval = interval;
          frameTimer.Tick += new EventHandler(advanceFrame);
          frameTimer.Start();
     }

     public void Start() {
          Start(100);
     }

     public void Stop() {
          frameTimer.Stop();
          frameTimer.Dispose();
     }

     public Bitmap Paint(Graphics e) {
          Bitmap temp;
          Graphics tempGraphics;

          temp = new Bitmap(width, height, e);
          tempGraphics = Graphics.FromImage(temp);

          tempGraphics.DrawImageUnscaled(img, 0-(width*frame), 0);

          tempGraphics.Dispose();
          return(temp);
     }

     private void advanceFrame(Object sender, EventArgs e) {
          frame++;
          if ( frame >= img.Width/width )
               frame = 0;
          }
     }
}

如何使用此类使我的gif文件(running_dog.gif)从左到右运行在background.jpg上?

这是dog.gif文件:dog.gif

1 个答案:

答案 0 :(得分:2)

您所包含的课程要求动画帧从左到右而不是从.gif开始从上到下。

您可以通过将构造函数更改为

来更改它
public AnimSprite(string f_imgFile, int f_height) {
    frame = 0;
    height = f_height;
    imgFile = f_imgFile;

    img = new Bitmap(imgFile);
    width = img.Width;
}

和advanceFrame方法

private void advanceFrame(Object sender, EventArgs e) {
    frame++;
    if ( frame >= img.Height/height )
       frame = 0;
    }
}

您对DrawImageUnscaled的调用

tempGraphics.DrawImageUnscaled(img, 0, 0-(height*frame));