Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:10:54+00:00 2026-06-13T01:10:54+00:00

I have a gif animation. As I’m working in CompactFramework I followed this MSDN

  • 0

I have a gif animation. As I’m working in CompactFramework I followed this MSDN example.

The basic idea is to add the frames to an image and redraw a rectangle with the next frame, creating the animation. It works just fine except that the animation is displaying in a square, not in the full size of my image(which is 240,320).

This is my code:

in a class AnimateCtl.cs:

public class AnimateCtl : System.Windows.Forms.Control
{
    Timer fTimer;
    int frameWidth = 240;
    int frameHeight = 320;
    int loopCount = 0;
    int loopCounter = 0;
    int frameCount;
    int currentFrame = 0;
    Graphics graphics;

    private Bitmap bitmap;
    public Bitmap Bitmap
    {
        get
        {
            return bitmap;
        }
        set
        {
            bitmap = value;
        }
    }

    private void Draw(int iframe)
    {
        //Calculate the left location of the drawing frame
        int XLocation = iframe * frameWidth;

        Rectangle rect = new Rectangle(XLocation, 0, frameWidth, frameHeight);

        //Draw image
        graphics.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
    }

    public AnimateCtl()
    {
        //Cache the Graphics object
        graphics = this.CreateGraphics();
        //Instantiate the Timer
        fTimer = new System.Windows.Forms.Timer();
        //Hook up to the Timer's Tick event
        fTimer.Tick += new System.EventHandler(this.timer1_Tick);
    }

    /// <summary>
    /// Start animation
    /// </summary>
    /// <param name="frWidth"></param>
    /// <param name="DelayInterval"></param>
    /// <param name="LoopCount"></param>
    public void StartAnimation(int frWidth, int DelayInterval, int LoopCount)
    {

        frameWidth = frWidth;
        //How many times to loop
        loopCount = LoopCount;
        //Reset loop counter
        loopCounter = 0;
        //Calculate the frameCount
        frameCount = bitmap.Width / frameWidth;
        frameHeight = bitmap.Height;
        //Resize the control
        //this.Size(frameWidth, frameHeight);
        //Assign delay interval to the timer
        fTimer.Interval = DelayInterval;
        //Start the timer
        fTimer.Enabled = true;
    }

    private void timer1_Tick(object sender, System.EventArgs e)
    {
        if (loopCount == -1) //loop continuously
        {
            this.DrawFrame();
        }
        else
        {
            if (loopCount == loopCounter) //stop the animation
                fTimer.Enabled = false;
            else
                this.DrawFrame();
        }
    }

    private void DrawFrame()
    {
        if (currentFrame < frameCount - 1)
        {
            //move to the next frame
            currentFrame++;
        }
        else
        {
            //increment the loopCounter
            loopCounter++;
            currentFrame = 0;
        }
        Draw(currentFrame);
    }

and in my Form1:

public partial class Form1 : Form
{
    AnimateCtl animCtl;

    public Form1()
    {
        InitializeComponent();

        //Instantiate control
        animCtl = new AnimateCtl();
        //Assign the Bitmap from the image file
        animCtl.Bitmap = new Bitmap(@"\Program Files\Animacion\Animacion-Frames.jpg");
        //Set the location
        animCtl.Location = new Point(0, 0);
        //Add to control to the Form
        this.Controls.Add(animCtl);

        animCtl.StartAnimation(240, 100, 3);
    }        
}

Why it isn’t drawing the rectangle in the correct dimension?
Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:10:55+00:00Added an answer on June 13, 2026 at 1:10 am

    remove the Draw methode and update the DrawFrame like this

        private void DrawFrame() {
            if (currentFrame < frameCount - 1) {
                currentFrame++;
            } else {
                loopCounter++;
                currentFrame = 0;
            }
            this.Invalidate();
        }
    
        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
    
            int XLocation = currentFrame * frameWidth;
            Rectangle rect = new Rectangle(XLocation, 0, frameWidth, frameHeight);
            e.Graphics.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
        }
    

    don’t ever use this.CreateGraphics(). overwriting the OnPaint methode is the right way. OnPaint will always fired after you call this.Invalidate();.

    With this.CreateGraphics() you only got the current graphics size, but after the form has shown the control size probably changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do have a gif image load bar animation I do want it to
I have a page that has a gif animation in a hidden div. Upon
I have a .gif file which i wish to use as the background image
I have a gif image 720 * 40 pixels used as a footer on
I have an animated gif image that gets swapped to another animated gif onclick.
I have a gif animation that will loop forever. However, I don't want it
If I have an animated gif that just runs a single animation, IE. it
I would like to use a GIF image as a loading animation on my
I'm looking for a .NET C# gif animation library (doesn't have to be free)
I have a web page with a 13 second animation involving several gif and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.