1

我找到了一个类来制作一个包含多帧动画的 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

4

1 回答 1

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));
于 2009-12-03T14:55:17.123 回答