views:

712

answers:

1

I found a class to make a gif file containing multiple frame animations run in front of a background image. This is my class:

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;
          }
     }
}

How can I use this class to make my gif file (running_dog.gif) run over background.jpg from left to right?

This is the dog.gif file: dog.gif

+2  A: 

The class you've included expects the animation frames to go from left to right rather than top to bottom as your .gif does.

You can alter it by changing the constructor to

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

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

and the advanceFrame method to

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

and your call to DrawImageUnscaled to

tempGraphics.DrawImageUnscaled(img, 0, 0-(height*frame));
jammus
It has to go from left to right and every step is a different frame in the gif file. So basically it has to go from feft to right and from top to bottom (in the gif) at the same time.
THDBASED
Thanks for your help! Now I get a divide by zero exception though.
THDBASED
Just to be sure, this is the way to call it no?AnimSprites.AnimSprite P = new AnimSprites.AnimSprite("running_dog.gif", 100);P.Start();
THDBASED
That looks right to me. You still getting the divide by zero?
jammus
Ooops. Just noticed a bug in the constructor. Have updated my answer.
jammus
I don't get the error any more no, but the dog is still not running :) I looked at the values while debugging but can't find the error.
THDBASED
how are you drawing the dog? How often are you drawing him (or her)?
jammus
I guess the Paint method has to be looped, that is the part I don't know how to do.
THDBASED
any thoughts, someone?
THDBASED